170 likes | 278 Views
Application. Note that “ | ” is synonymous with “ U ” in regular expressions. Why have different representation formalisms for the same class of languages? Convenience of expression
E N D
Application Note that “|” is synonymous with “U” in regular expressions. L5Appl
Why have different representation formalisms for the same class of languages? • Convenience of expression • For certain languages, a regular grammar may be substantially easier to write than a regular expression (or vice versa). • Regular expression can be mapped to a recognizer in a language containing sequencing, conditional, and while-loop. • Regular grammars can be mapped to finite automaton. L5Appl
Example RE difficult L5Appl
Example RE difficult Requires simulation of complement using union, concatenation, and Kleene-star operation, which is not straightforward. (Refer to: Finite State Automata results.) complement L5Appl
Closure properties • Regular languages are closed under union, concatenation, and Kleene-star, by definition. However, closure under complementation and intersection is not obvious. • Robustness of Regular Languages • Various formalisms (such as regular expressions, regular grammars, and finite automata) converge to defining the same collection of languages. “Intrinsic stability” of regular languages. L5Appl
Specifying and Automatically Generating Compiler Front-end (Refer to: LEX,FLEX,YACC,BISON) Lexical Analyzer Syntax Analyzer Back-end Regular Expressions Context-free Grammar Attribute Grammars L5Appl
Pattern Matching in Scripts and PLs • E.g., Regular expressions in awk, bash, UNIX grep,PERL, Python, Ruby, Java, .NET, etc • Document type definition (DTD) in XML (eXtensible Markup Language) • Specifies context-free syntax of a web-document using domain-specific vocabulary. • Document :: cs480.html :::: Language :: HTML :::: Meta-language :: SGML, XML L5Appl
Formal specification of Input and Output formats for programs during Software Development and Documentation. • Helpful to the users for clarification in user manuals. • Helpful to the designers in order to make I/O details explicit in a standard way. • Helpful to the developers, the integrators, and the testers for communication and clarification. L5Appl
Some URLs • LEX/YACC/FLEX/Bison: http://dinosaur.compilertools.net/ • BNF for Java http://cui.unige.ch/db-research/Enseignement/analyseinfo/JAVA/BNFindex.html • XML http://cm.bell-labs.com/cm/cs/who/wadler/xml/ L5Appl
Some URLs (Again!) URL for opening a web-page in a Browser • User’s concern: What is the syntax of the URL for Google’s homepage? • http://www.google.com/ soundness • Programmer’s concern: What are all syntactically legal URLs? (The following “URLs” work in several browsers!) • http://yahoo.com, yahoo.com • N:\tkprasad\cs466\HW.txt • file:///N:/tkprasad/cs466/HW.txt • ftp://www.cs.wright.edu • ftp://tkprasad@www.cs.wright.edu/ completeness L5Appl
Ambiguity in Grammars L5Appl
E E E E E E E E E E - - - - x x x x x x Parse/derivation trees for x - x - x Ambiguity: Two parse trees for x-x-x Two Interpretations: x, -x L5Appl
Using associativity information, it is possible to rewrite the grammar. • So, the language of expressions with x and – is not inherently ambiguous. Left associative: Right associative: L5Appl
Right associative a = b = 5; a = (b = 5); Left associative cout << 5 << “abc”; (cout << 5)<< “abc”; * has precedence over + 1 + 2 * 3 = 1 + (2 * 3) 1 + 2 * 3 =/= (1 + 2)*3 Associativity and precedence information is typically used to disambiguate non-fully parenthesized expressions containing unary prefix/postfix operators or binary infix operators. Operator Associativity and Precedences : Examples L5Appl
Dangling Else Problem Grammar: • if B1 thenif B2 then S1 else S2 • vs • if B1 thenif B2 then S1else S2 Ambiguity: L5Appl
Inherently ambiguous CFL • A context-free language that does not have an unambiguous context-free grammar is called an inherently ambiguous context-free language. L5Appl
Ambiguous Context-Free Grammar L5Appl