Syntactic Analysis and Context-Free Grammars
|
|
- Morgan Bruno Fleming
- 7 years ago
- Views:
Transcription
1 Syntactic Analysis and Context-Free Grammars CSCI 3136 Principles of Programming Languages Faculty of Computer Science Dalhousie University Winter 2012 Reading: Chapter 2
2 Motivation Source program (character stream) Scanner (lexical analysis) Front end Parse tree Parser (syntactic analysis) Token stream Back end Modified intermediate form Modified target language Semantic analysis and code generation Machine-independent code improvement Target code generation Machine-specific code improvement Abstract syntax tree or other intermediate form Target language (e.g., assembly) Symbol table
3 Grammars Describe Languages A grammar for a subset of natural language: S Phrase Verb Phrase Phrase Noun Phrase Adjective Phrase Adjective big green Noun cheese Jim Verb ate Sentences in the language described by this grammar: big Jim ate green cheese green Jim ate green cheese Jim ate cheese cheese at Jim
4 Another Example A grammar for simple arithmetic expressions: Expr ( Expr ) Expr Expr + Expr Expr ++ Expr Expr number Expr identifier
5 Context-Free Grammars A context-free grammar is a 4-tuple (V, Σ, P, S), where V is a set of non-terminals or variables, Σ is a set of terminals, P is a set of rules or productions in the form N (V Σ), where N V, S V is the start symbol. Expr ( Expr ) Expr Expr + Expr Expr ++ Expr Expr number Expr identifier Non-terminals: Expr Terminals: (, ), +, ++, number, identifier Rules: the rules on the left Start symbol: Expr
6 Notational Variations in Productions Merging alternatives using Phrase Noun Phrase Adjective Phrase Phrase Noun Adjective Phrase Backus-Naur form Phrase Noun Adjective Phrase Phrase ::= Noun Adjective Phrase Optional components FunctionDef DeclarationSpecifiers opt Declarator DeclarationList opt CompoundStatement Regular expression form of RHS Expression Term ( op Term )
7 Derivations A derivation is a sequence of rewriting operations that starts with the string σ = S and then repeats the following until σ contains only terminals: Choose a non-terminal X in σ and a production X α in the grammar and replace X with α in σ. As a picture σ = λx ρ σ = λαρ An example S Phrase Verb Phrase Phrase Verb Phrase Noun Verb Phrase Noun Verb Phrase Noun Verb Noun Noun Verb Noun Jim Verb Noun Jim Verb Noun Jim ate Noun Jim ate Noun Jim ate cheese Intermediate strings are called sentential forms.
8 Language Defined By a Context-Free Grammar We write S σ if there exists a sequence S σ 1 σ 2 σ Every grammar G defines a language (G) = {σ Σ S σ} If G is a context-free grammar, (G) is a context-free language. Example: The language defined by the Jim-ate-cheese grammar is (G) = {Jim ate cheese,jim ate Jim,big Jim ate cheese, }
9 Context-Free Languages: Examples What is the language defined by the following grammar? S ɛ S 0 S 0 S 1 S 1
10 Context-Free Languages: Examples What is the language defined by the following grammar? σ R is σ written backwards. S ɛ S 0 S 0 S 1 S 1 (G) = {σσ R σ Σ }
11 Context-Free Languages: Examples What is the language defined by the following grammar? σ R is σ written backwards. S ɛ S 0 S 0 S 1 S 1 (G) = {σσ R σ Σ } What is the language defined by the following grammar? S ɛ S 0 S 1
12 Context-Free Languages: Examples What is the language defined by the following grammar? σ R is σ written backwards. S ɛ S 0 S 0 S 1 S 1 (G) = {σσ R σ Σ } What is the language defined by the following grammar? S ɛ S 0 S 1 (G) = {0 n 1 n n 0}
13 Context-Free Languages: Examples What is the language defined by the following grammar? σ R is σ written backwards. S ɛ S 0 S 0 S 1 S 1 (G) = {σσ R σ Σ } What is the language defined by the following grammar? S ɛ S 0 S 1 (G) = {0 n 1 n n 0} Neither of these two languages is regular.
14 Context-Free Languages: Examples What is the language defined by the following grammar? σ R is σ written backwards. S ɛ S 0 S 0 S 1 S 1 (G) = {σσ R σ Σ } What is the language defined by the following grammar? S ɛ S 0 S 1 (G) = {0 n 1 n n 0} Neither of these two languages is regular. There are more context-free languages than regular ones.
15 Parse Trees Every derivation can be represented by a parse tree: The root is S. The children of each node are the symbols (terminals and non-terminals) it is replaced with. S Phrase Verb Phrase Noun Verb Phrase S Noun Verb Noun Phrase Verb Phrase Jim Verb Noun Jim ate Noun Jim ate cheese Noun Jim ate Noun cheese The internal nodes are non-terminals. The leaves called the yield of the parse-tree are terminals.
16 Parse Trees: Examples (1) S Phrase Verb Phrase Phrase Noun Phrase Adjective Phrase Adjective big green Noun cheese Jim Verb ate S Phrase Verb Phrase Adjective Phrase ate Noun big Noun cheese Jim
17 Parse Trees: Examples (2) S Phrase Verb Phrase Phrase Noun Phrase Adjective Phrase Adjective big green Noun cheese Jim Verb ate S Phrase Verb Phrase Adjective Phrase ate Adjective Phrase big Adjective Phrase green Noun big Noun cheese Jim
18 Ambiguity There are infinitely many context-free grammars generating a given context-free language. Just add arbitrary non-terminals to the right-hand sides of productions and then add ɛ-productions for these non-terminals.
19 Ambiguity There are infinitely many context-free grammars generating a given context-free language. Just add arbitrary non-terminals to the right-hand sides of productions and then add ɛ-productions for these non-terminals. There may be more than one parse tree for the same sentence generated by a grammar G. If this is the case, we call G ambiguous. To allow parsing of programming languages, their grammars have to be unambiguous.
20 Ambiguity There are infinitely many context-free grammars generating a given context-free language. Just add arbitrary non-terminals to the right-hand sides of productions and then add ɛ-productions for these non-terminals. There may be more than one parse tree for the same sentence generated by a grammar G. If this is the case, we call G ambiguous. To allow parsing of programming languages, their grammars have to be unambiguous. Some context-free languages are inherently ambiguous, that is, do not have unambiguous grammars. Usually, this is not the case for programming languages.
21 Ambiguity: Example (1) Expr number Expr identifier Expr Expr + Expr Expr Expr - Expr Expr Expr * Expr Expr Expr / Expr Expr ( Expr ) 2 + E E 3 * 4 E E *
22 Ambiguity: Example (1) Expr number Expr identifier Expr Expr + Expr Expr Expr - Expr Expr Expr * Expr Expr Expr / Expr Expr ( Expr ) 2 + E E 3 * 4 E E * Violates precedence rules.
23 Ambiguity: Example (2) An unambiguous grammar for the same language Expr Term Expr Expr + Term E Expr Expr - Term Term Factor E + T Term Term * Factor T T * F Term Term / Factor Factor number F F 4 Factor identifier Factor ( Expr ) 2 3
24 Ambiguity: Example (2) An unambiguous grammar for the same language Expr Term Expr Expr + Term E Expr Expr - Term Term Factor E + T Term Term * Factor T T * F Term Term / Factor Factor number F F 4 Factor identifier Factor ( Expr ) 2 3 This grammar respects precedence rules.
25 Ambiguity: Example (3) An unambiguous grammar for the same language Expr Term Expr Expr + Term Expr Expr - Term Term Factor Term Term * Factor Term Term / Factor Factor number Factor identifier Factor ( Expr ) E F 2 E E - T - T T F 3 2 F This grammar respects precedence rules. It also respects left-associativity.
26 Leftmost and Rightmost Derivations For every sentence in a language defined by an unambiguous grammar, there is only one parse tree that generates this sentence. There are many different derivations corresponding to this parse tree.
27 Leftmost and Rightmost Derivations For every sentence in a language defined by an unambiguous grammar, there is only one parse tree that generates this sentence. There are many different derivations corresponding to this parse tree. Leftmost derivation: Replaces the leftmost non-terminal in each step. E E + T T + T F + T E E + T 2 + T 2 + T * F T T * F 2 + F * F * F F F * 4 2 3
28 Leftmost and Rightmost Derivations For every sentence in a language defined by an unambiguous grammar, there is only one parse tree that generates this sentence. There are many different derivations corresponding to this parse tree. Leftmost derivation: Replaces the leftmost non-terminal in each step. Rightmost derivation: Replaces the rightmost non-terminal in each step. E E + T E E E + T T + T F + T 2 + T 2 + T * F 2 + F * F * F * 4 E T F 2 + T T F 3 * F 4 E + T * F E + T * 4 E + F * 4 E + 3 * 4 T + 3 * 4 F + 3 * * 4
29 Context-Free Grammars for Programming Languages Context-free grammars are used to formally describe the syntax of programming languages. Parsing a program involves finding the parse tree of the program.
30 Context-Free Grammars for Programming Languages Context-free grammars are used to formally describe the syntax of programming languages. Parsing a program involves finding the parse tree of the program. Context-free grammars for programming languages must be unambiguous and must capture the program structure.
31 Context-Free Grammars for Programming Languages Context-free grammars are used to formally describe the syntax of programming languages. Parsing a program involves finding the parse tree of the program. Context-free grammars for programming languages must be unambiguous and must capture the program structure. The parser should be efficient: Any context-free grammar can be used to derive a parser that runs in O(n 3 ) time. We want linear time.
32 Regular Grammars A context-free grammar is right-linear if all productions are of the form A σb or A σ, where σ is a (possibly empty) string of terminals.
33 Regular Grammars A context-free grammar is right-linear if all productions are of the form A σb or A σ, where σ is a (possibly empty) string of terminals. A context-free grammar is left-linear if all productions are of the form A Bσ or A σ, where σ is a (possibly empty) string of terminals.
34 Regular Grammars A context-free grammar is right-linear if all productions are of the form A σb or A σ, where σ is a (possibly empty) string of terminals. A context-free grammar is left-linear if all productions are of the form A Bσ or A σ, where σ is a (possibly empty) string of terminals. A context-free grammar is regular if it is right-linear or left-linear.
35 Regular Grammars A context-free grammar is right-linear if all productions are of the form A σb or A σ, where σ is a (possibly empty) string of terminals. A context-free grammar is left-linear if all productions are of the form A Bσ or A σ, where σ is a (possibly empty) string of terminals. A context-free grammar is regular if it is right-linear or left-linear. The set of languages expressed by regular grammars is exactly the set of regular languages. Regular grammars are too weak to express programming languages.
36 LL and LR Grammars Two kinds of grammars that can be parsed efficiently and guarantee unambiguousness:
37 LL and LR Grammars Two kinds of grammars that can be parsed efficiently and guarantee unambiguousness: An LL(k)-grammar can be scanned Left-to-right and generates a Leftmost derivation. If the first letter in the current sentential form is a non-terminal, k tokens look-ahead in the input suffice to decide which production to use to expand it.
38 LL and LR Grammars Two kinds of grammars that can be parsed efficiently and guarantee unambiguousness: An LL(k)-grammar can be scanned Left-to-right and generates a Leftmost derivation. If the first letter in the current sentential form is a non-terminal, k tokens look-ahead in the input suffice to decide which production to use to expand it. An LR(k)-grammar can be scanned Left-to-right and generates a Rightmost derivation. The next k tokens in the input suffice to choose the next step the parser should perform. (We ll see what these steps are.)
39 LL and LR Grammars Two kinds of grammars that can be parsed efficiently and guarantee unambiguousness: An LL(k)-grammar can be scanned Left-to-right and generates a Leftmost derivation. If the first letter in the current sentential form is a non-terminal, k tokens look-ahead in the input suffice to decide which production to use to expand it. An LR(k)-grammar can be scanned Left-to-right and generates a Rightmost derivation. The next k tokens in the input suffice to choose the next step the parser should perform. (We ll see what these steps are.) Almost every programming language can be described by an LL(1)- or LR(1)-grammar.
40 S-Grammars An S-grammar or simple grammar is a special case of an LL(1)-grammar. A context-free grammar is an S-grammar if Every production starts with a terminal and The productions for the same LHS start with different terminals. An S-grammar A a A A A b A A non-simple context-free grammar A a A A A a A
41 LL Parsing: An Example An S-grammar for arithmetic expressions in Polish notation S + S S S - S S S * S S S / S S S neg S S integer (2 + 3) * *
42 LL Parsing: An Example An S-grammar for arithmetic expressions in Polish notation S S + S S S - S S S * S S S / S S S neg S S integer (2 + 3) * * S
43 LL Parsing: An Example An S-grammar for arithmetic expressions in Polish notation S + S S S - S S S * S S S / S S S neg S S integer (2 + 3) * * S + S S S + S S
44 LL Parsing: An Example An S-grammar for arithmetic expressions in Polish notation S + S S S - S S S * S S S / S S S neg S S integer (2 + 3) * * S + S S S + S S
45 LL Parsing: An Example An S-grammar for arithmetic expressions in Polish notation S + S S S - S S S * S S S / S S S neg S S integer (2 + 3) * * S + S S * S S S + S S * S S S
46 LL Parsing: An Example An S-grammar for arithmetic expressions in Polish notation S + S S S - S S S * S S S / S S S neg S S integer (2 + 3) * * S + S S * S S S + S S * S S S
47 LL Parsing: An Example An S-grammar for arithmetic expressions in Polish notation S + S S S - S S S * S S S / S S S neg S S integer (2 + 3) * * S + S S * S S + S S + S S * S S S + S S S S S
48 LL Parsing: An Example An S-grammar for arithmetic expressions in Polish notation S + S S S - S S S * S S S / S S S neg S S integer (2 + 3) * * S + S S * S S + S S + S S * S S S + S S S S S
49 LL Parsing: An Example An S-grammar for arithmetic expressions in Polish notation S + S S S - S S S * S S S / S S S neg S S integer (2 + 3) * * int S + S S * S S + S S S + S S * S S S + S S S S integer S S S
50 LL Parsing: An Example An S-grammar for arithmetic expressions in Polish notation S + S S S - S S S * S S S / S S S neg S S integer (2 + 3) * * int S + S S * S S + S S S + S S * S S S + S S S S integer S S S
51 LL Parsing: An Example An S-grammar for arithmetic expressions in Polish notation S + S S S - S S S * S S S / S S S neg S S integer (2 + 3) * * int S + S S * S S + S S int S + S S * S S S + S S S S integer S S S integer S S
52 LL Parsing: An Example An S-grammar for arithmetic expressions in Polish notation S + S S S - S S S * S S S / S S S neg S S integer (2 + 3) * * int S + S S * S S + S S int S + S S * S S S + S S S S integer S S S integer S S
53 LL Parsing: An Example An S-grammar for arithmetic expressions in Polish notation S + S S S - S S S * S S S / S S S neg S S integer (2 + 3) * * int S + S S * S S + S S int S + S S * S S S + S S S S integer S S S integer S S integer S int
54 LL Parsing: An Example An S-grammar for arithmetic expressions in Polish notation S + S S S - S S S * S S S / S S S neg S S integer (2 + 3) * * int S + S S * S S + S S int S + S S * S S S + S S S S integer S S S integer S S integer S int
55 LL Parsing: An Example An S-grammar for arithmetic expressions in Polish notation S + S S S - S S S * S S S / S S S neg S S integer (2 + 3) * * int S + S S * S S + S S int S + S S * S S S + S S S S integer S S S integer S S integer S integer int int
56 LL Parsing: An Example An S-grammar for arithmetic expressions in Polish notation S + S S S - S S S * S S S / S S S neg S S integer (2 + 3) * * int S + S S * S S + S S int S + S S * S S S + S S S S integer S S S integer S S integer S integer int int
57 LL Parsing: An Example An S-grammar for arithmetic expressions in Polish notation S + S S S - S S S * S S S / S S S neg S S integer (2 + 3) * * This process is called recursive descent parsing. int S + S S * S S + S S int S + S S * S S S + S S S S integer S S S integer S S integer S integer int int
58 LR Parsing: An Example A grammar for arithmetic expressions in reverse Polish notation S S S + S S S - S S S * S S S / S S neg S integer (2 + 3) * * 5 +
59 LR Parsing: An Example A grammar for arithmetic expressions in reverse Polish notation S S S + S S S - S S S * S S S / S S neg S integer (2 + 3) * * 5 +
60 LR Parsing: An Example A grammar for arithmetic expressions in reverse Polish notation S S S + S S S - S S S * S S S / S S neg S integer int (2 + 3) * * 5 + integer
61 LR Parsing: An Example A grammar for arithmetic expressions in reverse Polish notation S S S + S S S - S S S * S S S / S S neg S integer S int (2 + 3) * * 5 + integer S integer
62 LR Parsing: An Example A grammar for arithmetic expressions in reverse Polish notation S S S + S S S - S S S * S S S / S S neg S integer S int int (2 + 3) * * 5 + integer S integer
63 LR Parsing: An Example A grammar for arithmetic expressions in reverse Polish notation S S S + S S S - S S S * S S S / S S neg S integer S int S int (2 + 3) * * 5 + integer S integer S S +
64 LR Parsing: An Example A grammar for arithmetic expressions in reverse Polish notation S S S + S S S - S S S * S S S / S S neg S integer S int S int + (2 + 3) * * 5 + integer S integer S S +
65 LR Parsing: An Example A grammar for arithmetic expressions in reverse Polish notation S S S + S S S - S S S * S S S / S S neg S integer S int S S int + (2 + 3) * * 5 + integer S integer S S + S integer
66 LR Parsing: An Example A grammar for arithmetic expressions in reverse Polish notation S S S + S S S - S S S * S S S / S S neg S integer S int S S int + int (2 + 3) * * 5 + integer S integer S S + S integer
67 LR Parsing: An Example A grammar for arithmetic expressions in reverse Polish notation S S S + S S S - S S S * S S S / S S neg S integer S int S S int + S int (2 + 3) * * 5 + integer S integer S S + S integer S S *
68 LR Parsing: An Example A grammar for arithmetic expressions in reverse Polish notation S S S + S S S - S S S * S S S / S S neg S integer S int S S int + S int * (2 + 3) * * 5 + integer S integer S S + S integer S S *
69 LR Parsing: An Example A grammar for arithmetic expressions in reverse Polish notation S S S + S S S - S S S * S S S / S S neg S integer S int S S int S + S int * (2 + 3) * * 5 + integer S integer S S + S integer S S * S integer
70 LR Parsing: An Example A grammar for arithmetic expressions in reverse Polish notation S S S + S S S - S S S * S S S / S S neg S integer (2 + 3) * * 5 + S int S * S S int S int + int integer S integer S S + S integer S S * S integer
71 LR Parsing: An Example A grammar for arithmetic expressions in reverse Polish notation S S S + S S S - S S S * S S S / S S neg S integer (2 + 3) * * 5 + S int S integer S integer S S + S integer S S * S integer S S + S * S S int S int + int
72 LR Parsing: An Example A grammar for arithmetic expressions in reverse Polish notation S S S + S S S - S S S * S S S / S S neg S integer S int S S * S S int S int + int + (2 + 3) * * 5 + integer S integer S S + S integer S S * S integer S S +
73 LR Parsing: An Example A grammar for arithmetic expressions in reverse Polish notation S S S S + S S S - S S S * S S S / S S neg S integer S int S S * S S int S int + int + (2 + 3) * * 5 + integer S integer S S + S integer S S * S integer S S + S
74 LR Parsing: An Example A grammar for arithmetic expressions in reverse Polish notation S S S S + S S S - S S S * S S S / S S neg S integer S int S S * S S int S int + int + (2 + 3) * * 5 + This process is called shift-reduce parsing. integer S integer S S + S integer S S * S integer S S + S
75 Parsing Using LL(1) Grammars The crux with parsing deterministically using an LL(1) grammar is to decide which production to apply when the next symbol in the current sentential form is a non-terminal: Input: a Sentential form: A Production: A α?
76 Parsing Using LL(1) Grammars The crux with parsing deterministically using an LL(1) grammar is to decide which production to apply when the next symbol in the current sentential form is a non-terminal: Input: a Sentential form: A Production: A α? Two cases A α aβ A α ɛ and a derivation of A can be succeeded by a.
77 Parsing Using LL(1) Grammars The crux with parsing deterministically using an LL(1) grammar is to decide which production to apply when the next symbol in the current sentential form is a non-terminal: Input: a Sentential form: A Production: A α? Two cases A α aβ A α ɛ and a derivation of A can be succeeded by a. Intuitively (but formally not quite correctly), a terminal a is in the predictor set of production A α if Aβ αβ aγ, for some β, γ Σ.
78 Parsing Using LL(1) Grammars The crux with parsing deterministically using an LL(1) grammar is to decide which production to apply when the next symbol in the current sentential form is a non-terminal: Input: a Sentential form: A Production: A α? Two cases A α aβ A α ɛ and a derivation of A can be succeeded by a. Intuitively (but formally not quite correctly), a terminal a is in the predictor set of production A α if Aβ αβ aγ, for some β, γ Σ. A grammar is LL(1) if the predictor sets of all productions with the same LHS are disjoint.
79 Parsing Using LL(1) Grammars The crux with parsing deterministically using an LL(1) grammar is to decide which production to apply when the next symbol in the current sentential form is a non-terminal: Input: a Sentential form: A Production: A α? Two cases A α aβ A α ɛ and a derivation of A can be succeeded by a. Intuitively (but formally not quite correctly), a terminal a is in the predictor set of production A α if Aβ αβ aγ, for some β, γ Σ. A grammar is LL(1) if the predictor sets of all productions with the same LHS are disjoint. Rule Predictor set S + S S {+} S - S S {-} S * S S {*} S / S S {/} S neg S {neg} S integer {integer}
80 Computing Predictor Sets Compute three kinds of sets: FIRST(σ), for all σ (V Σ) : For a Σ, a FIRST(σ) if σ aβ. ɛ FIRST(σ) if σ ɛ. (We compute FIRST(X ) only for X V Σ and generate FIRST(σ), for some strings σ (V Σ), on the fly when needed.)
81 Computing Predictor Sets Compute three kinds of sets: FIRST(σ), for all σ (V Σ) : For a Σ, a FIRST(σ) if σ aβ. ɛ FIRST(σ) if σ ɛ. (We compute FIRST(X ) only for X V Σ and generate FIRST(σ), for some strings σ (V Σ), on the fly when needed.) FOLLOW(X), for all X V : For a Σ, a FOLLOW(X ) if S αx aβ. ɛ FOLLOW(X ) if S αx.
82 Computing Predictor Sets Compute three kinds of sets: FIRST(σ), for all σ (V Σ) : For a Σ, a FIRST(σ) if σ aβ. ɛ FIRST(σ) if σ ɛ. (We compute FIRST(X ) only for X V Σ and generate FIRST(σ), for some strings σ (V Σ), on the fly when needed.) FOLLOW(X), for all X V : For a Σ, a FOLLOW(X ) if S αx aβ. ɛ FOLLOW(X ) if S αx. PREDICT(A α): For a Σ {ɛ}, a PREDICT(A α) if a FIRST(α) \ {ɛ} or ɛ FIRST(α) and a FOLLOW(A).
83 Computing Predictor Sets Compute three kinds of sets: FIRST(σ), for all σ (V Σ) : For a Σ, a FIRST(σ) if σ aβ. ɛ FIRST(σ) if σ ɛ. (We compute FIRST(X ) only for X V Σ and generate FIRST(σ), for some strings σ (V Σ), on the fly when needed.) FOLLOW(X), for all X V : For a Σ, a FOLLOW(X ) if S αx aβ. ɛ FOLLOW(X ) if S αx. PREDICT(A α): For a Σ {ɛ}, a PREDICT(A α) if a FIRST(α) \ {ɛ} or ɛ FIRST(α) and a FOLLOW(A).
84 Computing FIRST Computing FIRST(X), for X V Σ: For a Σ, FIRST(a) := {a}. For X V, FIRST(X ) :=. Repeat until no FIRST(X ) set changes any more: FIRST(X ) := FIRST(X ) FIRST(Y 1 Y 2 Y k ), for each production X Y 1 Y 2 Y k. Computing FIRST(Y 1 Y 2 Y k ) on the fly when needed: FIRST(Y 1 Y 2 Y k ) := For i := 1, 2,, k: FIRST(Y 1 Y 2 Y k ) := FIRST(Y 1 Y 2 Y k ) (FIRST(Y i ) \ {ɛ}) If ɛ / FIRST(Y i ), then return. FIRST(Y 1 Y 2 Y k ) := FIRST(Y 1 Y 2 Y k ) {ɛ}
85 Computing FIRST: Example Is the following grammar LL(1)? T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f
86 Computing FIRST: Example Is the following grammar LL(1)? T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f Symbol X p q b e c f T A P Q B C FIRST(X) It. 1 It. 2 It. 3 It. 4
87 Computing FIRST: Example Is the following grammar LL(1)? T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f Symbol X p q b e c f T A P Q B C It. 1 {p} {q} {b} {e} {c} {f } FIRST(X) It. 2 It. 3 It. 4
88 Computing FIRST: Example Is the following grammar LL(1)? T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f Symbol X p q b e c f T A P Q B C It. 1 {p} {q} {b} {e} {c} {f } FIRST(X) It. 2 It. 3 It. 4
89 Computing FIRST: Example Is the following grammar LL(1)? T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f Symbol X p q b e c f T A P Q B C It. 1 {p} {q} {b} {e} {c} {f } It. 2 FIRST(X) It. 3 It. 4
90 Computing FIRST: Example Is the following grammar LL(1)? T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f Symbol X p q b e c f T A P Q B C It. 1 {p} {q} {b} {e} {c} {f } It. 2 {p, ɛ} {q, ɛ} {b, e} {c, f } FIRST(X) It. 3 It. 4
91 Computing FIRST: Example Is the following grammar LL(1)? T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f Symbol X p q b e c f T A P Q B C It. 1 {p} {q} {b} {e} {c} {f } It. 2 {p, ɛ} {q, ɛ} {b, e} {c, f } FIRST(X) It. 3 It. 4
92 Computing FIRST: Example Is the following grammar LL(1)? T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f Symbol X p q b e c f T A P Q B C It. 1 {p} {q} {b} {e} {c} {f } It. 2 {p, ɛ} {q, ɛ} {b, e} {c, f } FIRST(X) It. 3 {p, q, ɛ, b, e} It. 4
93 Computing FIRST: Example Is the following grammar LL(1)? T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f Symbol X p q b e c f T A P Q B C It. 1 {p} {q} {b} {e} {c} {f } It. 2 {p, ɛ} {q, ɛ} {b, e} {c, f } FIRST(X) It. 3 {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } It. 4
94 Computing FIRST: Example Is the following grammar LL(1)? T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f Symbol X p q b e c f T A P Q B C It. 1 {p} {q} {b} {e} {c} {f } It. 2 {p, ɛ} {q, ɛ} {b, e} {c, f } FIRST(X) It. 3 {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } It. 4 {p, q, b, e}
95 Computing FIRST: Example Is the following grammar LL(1)? T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f Symbol X p q b e c f T A P Q B C It. 1 {p} {q} {b} {e} {c} {f } It. 2 {p, ɛ} {q, ɛ} {b, e} {c, f } FIRST(X) It. 3 {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } It. 4 {p, q, b, e} {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f }
96 Computing FIRST: Example Is the following grammar LL(1)? T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f Symbol X p q b e c f T A P Q B C It. 1 {p} {q} {b} {e} {c} {f } It. 2 {p, ɛ} {q, ɛ} {b, e} {c, f } FIRST(X) It. 3 {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } It. 4 {p} {q} {b} {e} {c} {f } {p, q, b, e} {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f }
97 Computing Predictor Sets Compute three kinds of sets: FIRST(σ), for all σ (V Σ) : For a Σ, a FIRST(σ) if σ aβ. ɛ FIRST(σ) if σ ɛ. (We compute FIRST(X ) only for X V Σ and generate FIRST(σ), for some strings σ (V Σ), on the fly when needed.) FOLLOW(X), for all X V : For a Σ, a FOLLOW(X ) if S αx aβ. ɛ FOLLOW(X ) if S αx. PREDICT(A α): For a Σ {ɛ}, a PREDICT(A α) if a FIRST(α) \ {ɛ} or ɛ FIRST(α) and a FOLLOW(A).
98 Computing FOLLOW FOLLOW(S) := {ɛ} FOLLOW(X ) :=, for all X V \ {S} Repeat until no FOLLOW(X ) set changes any more: For each production A αbβ: FOLLOW(B) := FOLLOW(B) (FIRST(β) \ {ɛ}) If ɛ FIRST(β), then FOLLOW(B) := FOLLOW(B) FOLLOW(A)
99 Computing FOLLOW: Example T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f X p q b e c f T A P Q B C FIRST(X) {p} {q} {b} {e} {c} {f } {p, q, b, e} {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } V T A P Q B C FOLLOW(V) It. 1 It. 2 It. 3
100 Computing FOLLOW: Example T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f X p q b e c f T A P Q B C FIRST(X) {p} {q} {b} {e} {c} {f } {p, q, b, e} {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } V T A P Q B C It. 1 {ɛ} FOLLOW(V) It. 2 It. 3
101 Computing FOLLOW: Example T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f X p q b e c f T A P Q B C FIRST(X) {p} {q} {b} {e} {c} {f } {p, q, b, e} {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } V T A P Q B C It. 1 {ɛ} FOLLOW(V) It. 2 It. 3 {ɛ}
102 Computing FOLLOW: Example T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f X p q b e c f T A P Q B C FIRST(X) {p} {q} {b} {e} {c} {f } {p, q, b, e} {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } V T A P Q B C It. 1 {ɛ} FOLLOW(V) It. 2 It. 3 {ɛ} {b, e}
103 Computing FOLLOW: Example T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f X p q b e c f T A P Q B C FIRST(X) {p} {q} {b} {e} {c} {f } {p, q, b, e} {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } V T A P Q B C It. 1 {ɛ} FOLLOW(V) It. 2 It. 3 {ɛ} {b, e} {q}
104 Computing FOLLOW: Example T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f X p q b e c f T A P Q B C FIRST(X) {p} {q} {b} {e} {c} {f } {p, q, b, e} {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } V T A P Q B C It. 1 {ɛ} FOLLOW(V) It. 2 It. 3 {ɛ} {b, e} {q}
105 Computing FOLLOW: Example T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f X p q b e c f T A P Q B C FIRST(X) {p} {q} {b} {e} {c} {f } {p, q, b, e} {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } V T A P Q B C It. 1 {ɛ} FOLLOW(V) It. 2 It. 3 {ɛ} {b, e} {q} {ɛ, c, f }
106 Computing FOLLOW: Example T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f X p q b e c f T A P Q B C FIRST(X) {p} {q} {b} {e} {c} {f } {p, q, b, e} {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } V T A P Q B C It. 1 {ɛ} FOLLOW(V) It. 2 It. 3 {ɛ} {b, e} {q} {ɛ, c, f }
107 Computing FOLLOW: Example T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f X p q b e c f T A P Q B C FIRST(X) {p} {q} {b} {e} {c} {f } {p, q, b, e} {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } V T A P Q B C It. 1 {ɛ} FOLLOW(V) It. 2 It. 3 {ɛ} {b, e} {q} {ɛ, c, f } {ɛ} {b, e}
108 Computing FOLLOW: Example T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f X p q b e c f T A P Q B C FIRST(X) {p} {q} {b} {e} {c} {f } {p, q, b, e} {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } V T A P Q B C It. 1 {ɛ} FOLLOW(V) It. 2 It. 3 {ɛ} {b, e} {q} {ɛ, c, f } {ɛ} {b, e} {q, b, e}
109 Computing FOLLOW: Example T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f X p q b e c f T A P Q B C FIRST(X) {p} {q} {b} {e} {c} {f } {p, q, b, e} {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } V T A P Q B C It. 1 {ɛ} FOLLOW(V) It. 2 It. 3 {ɛ} {b, e} {q} {ɛ, c, f } {ɛ} {b, e} {q, b, e} {b, e}
110 Computing FOLLOW: Example T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f X p q b e c f T A P Q B C FIRST(X) {p} {q} {b} {e} {c} {f } {p, q, b, e} {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } V T A P Q B C It. 1 {ɛ} FOLLOW(V) It. 2 It. 3 {ɛ} {b, e} {q} {ɛ, c, f } {ɛ} {b, e} {q, b, e} {b, e} {ɛ, c, f }
111 Computing FOLLOW: Example T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f X p q b e c f T A P Q B C FIRST(X) {p} {q} {b} {e} {c} {f } {p, q, b, e} {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } V T A P Q B C It. 1 {ɛ} FOLLOW(V) It. 2 It. 3 {ɛ} {b, e} {q} {ɛ, c, f } {ɛ} {b, e} {q, b, e} {b, e} {ɛ, c, f } {b, e}
112 Computing Predictor Sets Compute three kinds of sets: FIRST(σ), for all σ (V Σ) : For a Σ, a FIRST(σ) if σ aβ. ɛ FIRST(σ) if σ ɛ. (We compute FIRST(X ) only for X V Σ and generate FIRST(σ), for some strings σ (V Σ), on the fly when needed.) FOLLOW(X), for all X V : For a Σ, a FOLLOW(X ) if S αx aβ. ɛ FOLLOW(X ) if S αx. PREDICT(A α): For a Σ {ɛ}, a PREDICT(A α) if a FIRST(α) \ {ɛ} or ɛ FIRST(α) and a FOLLOW(A).
113 Computing PREDICT PREDICT(A α) :=, for every production A α For every production A α: PREDICT(A α) := FIRST(α) \ {ɛ} If ɛ FIRST(α), then PREDICT(A α) := PREDICT(A α) FOLLOW(A)
114 Computing PREDICT: Example X p q b e c f T A P Q B C FIRST(X) {p} {q} {b} {e} {c} {f } {p, q, b, e} {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } V T A P Q B C FOLLOW(V) {ɛ} {b, e} {q, b, e} {b, e} {ɛ, c, f } {b, e} Rule R T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f PREDICT(R)
115 Computing PREDICT: Example X p q b e c f T A P Q B C FIRST(X) {p} {q} {b} {e} {c} {f } {p, q, b, e} {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } V T A P Q B C FOLLOW(V) {ɛ} {b, e} {q, b, e} {b, e} {ɛ, c, f } {b, e} Rule R T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f PREDICT(R) {p, q, b, e}
116 Computing PREDICT: Example X p q b e c f T A P Q B C FIRST(X) {p} {q} {b} {e} {c} {f } {p, q, b, e} {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } V T A P Q B C FOLLOW(V) {ɛ} {b, e} {q, b, e} {b, e} {ɛ, c, f } {b, e} Rule R T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f PREDICT(R) {p, q, b, e} {p, q, b, e}
117 Computing PREDICT: Example X p q b e c f T A P Q B C FIRST(X) {p} {q} {b} {e} {c} {f } {p, q, b, e} {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } V T A P Q B C FOLLOW(V) {ɛ} {b, e} {q, b, e} {b, e} {ɛ, c, f } {b, e} Rule R T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f PREDICT(R) {p, q, b, e} {p, q, b, e} {b, e}
118 Computing PREDICT: Example X p q b e c f T A P Q B C FIRST(X) {p} {q} {b} {e} {c} {f } {p, q, b, e} {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } V T A P Q B C FOLLOW(V) {ɛ} {b, e} {q, b, e} {b, e} {ɛ, c, f } {b, e} Rule R T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f PREDICT(R) {p, q, b, e} {p, q, b, e} {b, e} {p}
119 Computing PREDICT: Example X p q b e c f T A P Q B C FIRST(X) {p} {q} {b} {e} {c} {f } {p, q, b, e} {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } V T A P Q B C FOLLOW(V) {ɛ} {b, e} {q, b, e} {b, e} {ɛ, c, f } {b, e} Rule R T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f PREDICT(R) {p, q, b, e} {p, q, b, e} {b, e} {p} {q, b, e}
120 Computing PREDICT: Example X p q b e c f T A P Q B C FIRST(X) {p} {q} {b} {e} {c} {f } {p, q, b, e} {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } V T A P Q B C FOLLOW(V) {ɛ} {b, e} {q, b, e} {b, e} {ɛ, c, f } {b, e} Rule R T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f PREDICT(R) {p, q, b, e} {p, q, b, e} {b, e} {p} {q, b, e} {q}
121 Computing PREDICT: Example X p q b e c f T A P Q B C FIRST(X) {p} {q} {b} {e} {c} {f } {p, q, b, e} {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } V T A P Q B C FOLLOW(V) {ɛ} {b, e} {q, b, e} {b, e} {ɛ, c, f } {b, e} Rule R T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f PREDICT(R) {p, q, b, e} {p, q, b, e} {b, e} {p} {q, b, e} {q} {b, e}
122 Computing PREDICT: Example X p q b e c f T A P Q B C FIRST(X) {p} {q} {b} {e} {c} {f } {p, q, b, e} {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } V T A P Q B C FOLLOW(V) {ɛ} {b, e} {q, b, e} {b, e} {ɛ, c, f } {b, e} Rule R T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f PREDICT(R) {p, q, b, e} {p, q, b, e} {b, e} {p} {q, b, e} {q} {b, e} {b}
123 Computing PREDICT: Example X p q b e c f T A P Q B C FIRST(X) {p} {q} {b} {e} {c} {f } {p, q, b, e} {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } V T A P Q B C FOLLOW(V) {ɛ} {b, e} {q, b, e} {b, e} {ɛ, c, f } {b, e} Rule R T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f PREDICT(R) {p, q, b, e} {p, q, b, e} {b, e} {p} {q, b, e} {q} {b, e} {b} {e}
124 Computing PREDICT: Example X p q b e c f T A P Q B C FIRST(X) {p} {q} {b} {e} {c} {f } {p, q, b, e} {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } V T A P Q B C FOLLOW(V) {ɛ} {b, e} {q, b, e} {b, e} {ɛ, c, f } {b, e} Rule R T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f PREDICT(R) {p, q, b, e} {p, q, b, e} {b, e} {p} {q, b, e} {q} {b, e} {b} {e} {c}
125 Computing PREDICT: Example X p q b e c f T A P Q B C FIRST(X) {p} {q} {b} {e} {c} {f } {p, q, b, e} {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } V T A P Q B C FOLLOW(V) {ɛ} {b, e} {q, b, e} {b, e} {ɛ, c, f } {b, e} Rule R T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f PREDICT(R) {p, q, b, e} {p, q, b, e} {b, e} {p} {q, b, e} {q} {b, e} {b} {e} {c} {f }
126 Computing PREDICT: Example X p q b e c f T A P Q B C FIRST(X) {p} {q} {b} {e} {c} {f } {p, q, b, e} {p, q, ɛ, b, e} {p, ɛ} {q, ɛ} {b, e} {c, f } V T A P Q B C FOLLOW(V) {ɛ} {b, e} {q, b, e} {b, e} {ɛ, c, f } {b, e} Rule R T A B A P Q A B C P p P P ɛ Q q Q Q ɛ B b B B e C c C C f PREDICT(R) {p, q, b, e} {p, q, b, e} {b, e} {p} {q, b, e} {q} {b, e} {b} {e} {c} {f } The grammar is not LL(1)!
127 Some Facts About LL(1) Languages There exist context-free languages that do not have LL(1) grammars. There is no known algorithm to determine whether a language is LL(1) (but there is an algorithm to decide whether a grammar is LL(1)). The obvious grammar for most programming languages is usually not LL(1). In many situations, a non-ll(1) grammar can be transformed into an LL(1) grammar for the same language.
128 Converting a Grammar to LL(1) Two common reasons why a grammar is not LL(1) are left recursion and common prefixes, both of which can be eliminated by modifying the grammar. Left recursion A α Aβ Common prefix A αβ αγ Example of a common prefix Expr Term Expr Term + Expr
129 Removing Left Recursion and Common Prefixes Left recursion can be replaced with right recursion: A α Aβ A αa A ɛ βa Side effect: Often, left recursion is used intentionally to capture structure of the language (e.g., associativity of operators in arithmetic expressions). The above conversion removes this information.
130 Removing Left Recursion and Common Prefixes Left recursion can be replaced with right recursion: A α Aβ A αa A ɛ βa Side effect: Often, left recursion is used intentionally to capture structure of the language (e.g., associativity of operators in arithmetic expressions). The above conversion removes this information. Common prefixes can be removed using left factoring: A αβ αγ A αa A β γ
131 Converting a Grammar to LL(1): Example (1) Rule (R) S E$ E EAT E T T T M F T F PREDICT(R) F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} V FOLLOW(V) S {ɛ} E {$,),+,-} A T {$,),+,-,*,/} M F {$,),+,-,*,/} X FIRST(X) $ {$} n {n} ( {(} ) {)} + {+} - {-} * {*} / {/} S E A {+,-} T M {*,/} F
132 Converting a Grammar to LL(1): Example (2) Rule (R) S E$ E T E PREDICT(R) E ɛ {$,)} E AT E {+,-} T F T T ɛ {+,-, $,)} T M F T {*,/} F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} V S E E A T T M F FOLLOW(V) {ɛ} {$,)} {$,)} {+,-, $,)} {+,-, $,)} {*,/,+,-, $, )} X FIRST(X) $ {$} n {n} ( {(} ) {)} + {+} - {-} * {*} / {/} S E E {ɛ,+,-} A {+,-} T T {ɛ,*,/} M {*,/} F
133 Parsing LL(1) Languages LL(1) languages can be parsed using efficient, easy-to-implement parsers. Two approaches Recursive descent method Deterministic push-down automaton (DPDA) Recursive descent parsing For each non-terminal X, write a procedure parse-x: Based on next token, choose production X Y 1 Y 2 Y k If Y 1 is a non-terminal, then parse-y 1, else match Y 1 with next input token. If Y 2 is a non-terminal, then parse-y 2, else match Y 2 with next input token.
134 Recursive Descent Parsing: Example Rule (R) S E$ E T E PREDICT(R) E ɛ {$,)} E AT E {+,-} T F T T ɛ {+,-, $,)} T M F T {*,/} F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} 2 * * 3 $ parse-s If next token is n or (, then parse-e Match $
135 Recursive Descent Parsing: Example Rule (R) S E$ E T E PREDICT(R) E ɛ {$,)} E AT E {+,-} T F T T ɛ {+,-, $,)} T M F T {*,/} F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} 2 * * 3 $ parse-s If next token is n or (, then parse-e Match $ parse-e If next token is n or (, then parse-t parse-e
136 Recursive Descent Parsing: Example Rule (R) S E$ E T E PREDICT(R) E ɛ {$,)} E AT E {+,-} T F T T ɛ {+,-, $,)} T M F T {*,/} F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} 2 * * 3 $ parse-s If next token is n or (, then parse-e Match $ parse-e If next token is n or (, then parse-t parse-e parse-t If next token is n or (, then parse-f parse-t
137 Recursive Descent Parsing: Example Rule (R) S E$ E T E PREDICT(R) E ɛ {$,)} E AT E {+,-} T F T T ɛ {+,-, $,)} T M F T {*,/} F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} 2 * * 3 $ parse-s If next token is n or (, then parse-e Match $ parse-e If next token is n or (, then parse-t parse-e parse-t If next token is n or (, then parse-f parse-t parse-f If next token is n, then Match n
138 Recursive Descent Parsing: Example Rule (R) S E$ E T E PREDICT(R) E ɛ {$,)} E AT E {+,-} T F T T ɛ {+,-, $,)} T M F T {*,/} F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} 2 * * 3 $ parse-s If next token is n or (, then parse-e Match $ parse-e If next token is n or (, then parse-t parse-e parse-t If next token is n or (, then parse-f parse-t parse-f If next token is n, then Match n
139 Recursive Descent Parsing: Example Rule (R) S E$ E T E PREDICT(R) E ɛ {$,)} E AT E {+,-} T F T T ɛ {+,-, $,)} T M F T {*,/} F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} 2 * * 3 $ parse-s If next token is n or (, then parse-e Match $ parse-e If next token is n or (, then parse-t parse-e parse-t If next token is n or (, then parse-f parse-t
140 Recursive Descent Parsing: Example Rule (R) S E$ E T E PREDICT(R) E ɛ {$,)} E AT E {+,-} T F T T ɛ {+,-, $,)} T M F T {*,/} F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} 2 * * 3 $ parse-s If next token is n or (, then parse-e Match $ parse-e If next token is n or (, then parse-t parse-e parse-t If next token is n or (, then parse-f parse-t parse-t If next token is * or /, then parse-m parse-f parse-t
141 Recursive Descent Parsing: Example Rule (R) S E$ E T E PREDICT(R) E ɛ {$,)} E AT E {+,-} T F T T ɛ {+,-, $,)} T M F T {*,/} F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} 2 * * 3 $ parse-s If next token is n or (, then parse-e Match $ parse-e If next token is n or (, then parse-t parse-e parse-t If next token is n or (, then parse-f parse-t parse-t If next token is * or /, then parse-m parse-f parse-t parse-m If next token is *, then Match *
142 Recursive Descent Parsing: Example Rule (R) S E$ E T E PREDICT(R) E ɛ {$,)} E AT E {+,-} T F T T ɛ {+,-, $,)} T M F T {*,/} F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} 2 * * 3 $ parse-s If next token is n or (, then parse-e Match $ parse-e If next token is n or (, then parse-t parse-e parse-t If next token is n or (, then parse-f parse-t parse-t If next token is * or /, then parse-m parse-f parse-t parse-m If next token is *, then Match *
143 Recursive Descent Parsing: Example Rule (R) S E$ E T E PREDICT(R) E ɛ {$,)} E AT E {+,-} T F T T ɛ {+,-, $,)} T M F T {*,/} F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} 2 * * 3 $ parse-s If next token is n or (, then parse-e Match $ parse-e If next token is n or (, then parse-t parse-e parse-t If next token is n or (, then parse-f parse-t parse-t If next token is * or /, then parse-m parse-f parse-t
144 Recursive Descent Parsing: Example Rule (R) S E$ E T E PREDICT(R) E ɛ {$,)} E AT E {+,-} T F T T ɛ {+,-, $,)} T M F T {*,/} F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} 2 * * 3 $ parse-s If next token is n or (, then parse-e Match $ parse-e If next token is n or (, then parse-t parse-e parse-t If next token is n or (, then parse-f parse-t parse-t If next token is * or /, then parse-m parse-f parse-t parse-f If next token is n, then Match n
145 Recursive Descent Parsing: Example Rule (R) S E$ E T E PREDICT(R) E ɛ {$,)} E AT E {+,-} T F T T ɛ {+,-, $,)} T M F T {*,/} F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} 2 * * 3 $ parse-s If next token is n or (, then parse-e Match $ parse-e If next token is n or (, then parse-t parse-e parse-t If next token is n or (, then parse-f parse-t parse-t If next token is * or /, then parse-m parse-f parse-t parse-f If next token is n, then Match n
146 Recursive Descent Parsing: Example Rule (R) S E$ E T E PREDICT(R) E ɛ {$,)} E AT E {+,-} T F T T ɛ {+,-, $,)} T M F T {*,/} F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} 2 * * 3 $ parse-s If next token is n or (, then parse-e Match $ parse-e If next token is n or (, then parse-t parse-e parse-t If next token is n or (, then parse-f parse-t parse-t If next token is * or /, then parse-m parse-f parse-t
147 Recursive Descent Parsing: Example Rule (R) S E$ E T E PREDICT(R) E ɛ {$,)} E AT E {+,-} T F T T ɛ {+,-, $,)} T M F T {*,/} F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} 2 * * 3 $ parse-s If next token is n or (, then parse-e Match $ parse-e If next token is n or (, then parse-t parse-e parse-t If next token is n or (, then parse-f parse-t parse-t If next token is * or /, then parse-m parse-f parse-t parse-t If next token is +, -, $ or ), then Do nothing
148 Recursive Descent Parsing: Example Rule (R) S E$ E T E PREDICT(R) E ɛ {$,)} E AT E {+,-} T F T T ɛ {+,-, $,)} T M F T {*,/} F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} 2 * * 3 $ parse-s If next token is n or (, then parse-e Match $ parse-e If next token is n or (, then parse-t parse-e parse-t If next token is n or (, then parse-f parse-t parse-t If next token is * or /, then parse-m parse-f parse-t
149 Recursive Descent Parsing: Example Rule (R) S E$ E T E PREDICT(R) E ɛ {$,)} E AT E {+,-} T F T T ɛ {+,-, $,)} T M F T {*,/} F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} 2 * * 3 $ parse-s If next token is n or (, then parse-e Match $ parse-e If next token is n or (, then parse-t parse-e parse-t If next token is n or (, then parse-f parse-t
150 Recursive Descent Parsing: Example Rule (R) S E$ E T E PREDICT(R) E ɛ {$,)} E AT E {+,-} T F T T ɛ {+,-, $,)} T M F T {*,/} F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} 2 * * 3 $ parse-s If next token is n or (, then parse-e Match $ parse-e If next token is n or (, then parse-t parse-e
151 Recursive Descent Parsing: Example Rule (R) S E$ E T E PREDICT(R) E ɛ {$,)} E AT E {+,-} T F T T ɛ {+,-, $,)} T M F T {*,/} F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} 2 * * 3 $ parse-s If next token is n or (, then parse-e Match $ parse-e If next token is n or (, then parse-t parse-e parse-e If next token is + or -, then parse-a parse-t parse-e
152 Recursive Descent Parsing: Example Rule (R) S E$ E T E PREDICT(R) E ɛ {$,)} E AT E {+,-} T F T T ɛ {+,-, $,)} T M F T {*,/} F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} 2 * * 3 $ parse-s If next token is n or (, then parse-e Match $ parse-e If next token is n or (, then parse-t parse-e parse-e If next token is + or -, then parse-a parse-t parse-e parse-a If next token is -, then Match -
153 Recursive Descent Parsing: Example Rule (R) S E$ E T E PREDICT(R) E ɛ {$,)} E AT E {+,-} T F T T ɛ {+,-, $,)} T M F T {*,/} F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} 2 * * 3 $ parse-s If next token is n or (, then parse-e Match $ parse-e If next token is n or (, then parse-t parse-e parse-e If next token is + or -, then parse-a parse-t parse-e parse-a If next token is -, then Match -
154 Recursive Descent Parsing: Example Rule (R) S E$ E T E PREDICT(R) E ɛ {$,)} E AT E {+,-} T F T T ɛ {+,-, $,)} T M F T {*,/} F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} 2 * * 3 $ parse-s If next token is n or (, then parse-e Match $ parse-e If next token is n or (, then parse-t parse-e parse-e If next token is + or -, then parse-a parse-t parse-e
155 Recursive Descent Parsing: Example Rule (R) S E$ E T E PREDICT(R) E ɛ {$,)} E AT E {+,-} T F T T ɛ {+,-, $,)} T M F T {*,/} F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} 2 * * 3 $ parse-s If next token is n or (, then parse-e Match $ parse-e If next token is n or (, then parse-t parse-e parse-e If next token is + or -, then parse-a parse-t parse-e parse-t If next token is n or (, then parse-f parse-t
156 Recursive Descent Parsing: Example Rule (R) S E$ E T E PREDICT(R) E ɛ {$,)} E AT E {+,-} T F T T ɛ {+,-, $,)} T M F T {*,/} F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} 2 * * 3 $ parse-s If next token is n or (, then parse-e Match $ parse-e If next token is n or (, then parse-t parse-e parse-e If next token is + or -, then parse-a parse-t parse-e parse-t If next token is n or (, then parse-f parse-t parse-f If next token is n, then Match n
157 Recursive Descent Parsing: Example Rule (R) S E$ E T E PREDICT(R) E ɛ {$,)} E AT E {+,-} T F T T ɛ {+,-, $,)} T M F T {*,/} F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} 2 * * 3 $ parse-s If next token is n or (, then parse-e Match $ parse-e If next token is n or (, then parse-t parse-e parse-e If next token is + or -, then parse-a parse-t parse-e parse-t If next token is n or (, then parse-f parse-t parse-f If next token is n, then Match n
158 Recursive Descent Parsing: Example Rule (R) S E$ E T E PREDICT(R) E ɛ {$,)} E AT E {+,-} T F T T ɛ {+,-, $,)} T M F T {*,/} F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} 2 * * 3 $ parse-s If next token is n or (, then parse-e Match $ parse-e If next token is n or (, then parse-t parse-e parse-e If next token is + or -, then parse-a parse-t parse-e parse-t If next token is n or (, then parse-f parse-t
159 Recursive Descent Parsing: Example Rule (R) S E$ E T E PREDICT(R) E ɛ {$,)} E AT E {+,-} T F T T ɛ {+,-, $,)} T M F T {*,/} F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} 2 * * 3 $ parse-s If next token is n or (, then parse-e Match $ parse-e If next token is n or (, then parse-t parse-e parse-e If next token is + or -, then parse-a parse-t parse-e parse-t If next token is n or (, then parse-f parse-t parse-t If next token is * or /, then parse-m parse-f parse-t
160 Recursive Descent Parsing: Example Rule (R) S E$ E T E PREDICT(R) E ɛ {$,)} E AT E {+,-} T F T T ɛ {+,-, $,)} T M F T {*,/} F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} 2 * * 3 $ parse-s If next token is n or (, then parse-e Match $ parse-e If next token is n or (, then parse-t parse-e parse-e If next token is + or -, then parse-a parse-t parse-e parse-t If next token is n or (, then parse-f parse-t parse-t If next token is * or /, then parse-m parse-f parse-t parse-m If next token is *, then Match *
161 Recursive Descent Parsing: Example Rule (R) S E$ E T E PREDICT(R) E ɛ {$,)} E AT E {+,-} T F T T ɛ {+,-, $,)} T M F T {*,/} F n {n} F (E) {(} A + {+} A - {-} M * {*} M / {/} 2 * * 3 $ parse-s If next token is n or (, then parse-e Match $ parse-e If next token is n or (, then parse-t parse-e parse-e If next token is + or -, then parse-a parse-t parse-e parse-t If next token is n or (, then parse-f parse-t parse-t If next token is * or /, then parse-m parse-f parse-t parse-m If next token is *, then Match *
FIRST and FOLLOW sets a necessary preliminary to constructing the LL(1) parsing table
FIRST and FOLLOW sets a necessary preliminary to constructing the LL(1) parsing table Remember: A predictive parser can only be built for an LL(1) grammar. A grammar is not LL(1) if it is: 1. Left recursive,
More informationSyntaktická analýza. Ján Šturc. Zima 208
Syntaktická analýza Ján Šturc Zima 208 Position of a Parser in the Compiler Model 2 The parser The task of the parser is to check syntax The syntax-directed translation stage in the compiler s front-end
More informationPushdown automata. Informatics 2A: Lecture 9. Alex Simpson. 3 October, 2014. School of Informatics University of Edinburgh als@inf.ed.ac.
Pushdown automata Informatics 2A: Lecture 9 Alex Simpson School of Informatics University of Edinburgh als@inf.ed.ac.uk 3 October, 2014 1 / 17 Recap of lecture 8 Context-free languages are defined by context-free
More informationScanning and parsing. Topics. Announcements Pick a partner by Monday Makeup lecture will be on Monday August 29th at 3pm
Scanning and Parsing Announcements Pick a partner by Monday Makeup lecture will be on Monday August 29th at 3pm Today Outline of planned topics for course Overall structure of a compiler Lexical analysis
More informationCOMP 356 Programming Language Structures Notes for Chapter 4 of Concepts of Programming Languages Scanning and Parsing
COMP 356 Programming Language Structures Notes for Chapter 4 of Concepts of Programming Languages Scanning and Parsing The scanner (or lexical analyzer) of a compiler processes the source program, recognizing
More informationCSCI 3136 Principles of Programming Languages
CSCI 3136 Principles of Programming Languages Faculty of Computer Science Dalhousie University Winter 2013 CSCI 3136 Principles of Programming Languages Faculty of Computer Science Dalhousie University
More informationIntroduction. Compiler Design CSE 504. Overview. Programming problems are easier to solve in high-level languages
Introduction Compiler esign CSE 504 1 Overview 2 3 Phases of Translation ast modifled: Mon Jan 28 2013 at 17:19:57 EST Version: 1.5 23:45:54 2013/01/28 Compiled at 11:48 on 2015/01/28 Compiler esign Introduction
More informationCompiler I: Syntax Analysis Human Thought
Course map Compiler I: Syntax Analysis Human Thought Abstract design Chapters 9, 12 H.L. Language & Operating Sys. Compiler Chapters 10-11 Virtual Machine Software hierarchy Translator Chapters 7-8 Assembly
More informationIf-Then-Else Problem (a motivating example for LR grammars)
If-Then-Else Problem (a motivating example for LR grammars) If x then y else z If a then if b then c else d this is analogous to a bracket notation when left brackets >= right brackets: [ [ ] ([ i ] j,
More informationUniversity of Toronto Department of Electrical and Computer Engineering. Midterm Examination. CSC467 Compilers and Interpreters Fall Semester, 2005
University of Toronto Department of Electrical and Computer Engineering Midterm Examination CSC467 Compilers and Interpreters Fall Semester, 2005 Time and date: TBA Location: TBA Print your name and ID
More informationAbout the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design
i About the Tutorial A compiler translates the codes written in one language to some other language without changing the meaning of the program. It is also expected that a compiler should make the target
More informationCompiler Construction
Compiler Construction Regular expressions Scanning Görel Hedin Reviderad 2013 01 23.a 2013 Compiler Construction 2013 F02-1 Compiler overview source code lexical analysis tokens intermediate code generation
More informationDesign Patterns in Parsing
Abstract Axel T. Schreiner Department of Computer Science Rochester Institute of Technology 102 Lomb Memorial Drive Rochester NY 14623-5608 USA ats@cs.rit.edu Design Patterns in Parsing James E. Heliotis
More informationCS143 Handout 08 Summer 2008 July 02, 2007 Bottom-Up Parsing
CS143 Handout 08 Summer 2008 July 02, 2007 Bottom-Up Parsing Handout written by Maggie Johnson and revised by Julie Zelenski. Bottom-up parsing As the name suggests, bottom-up parsing works in the opposite
More informationGrammars and Parsing. 2. A finite nonterminal alphabet N. Symbols in this alphabet are variables of the grammar.
4 Grammars and Parsing Formally a language is a set of finite-length strings over a finite alphabet. Because most interesting languages are infinite sets, we cannot define such languages by enumerating
More informationContext free grammars and predictive parsing
Context free grammars and predictive parsing Programming Language Concepts and Implementation Fall 2012, Lecture 6 Context free grammars Next week: LR parsing Describing programming language syntax Ambiguities
More informationBottom-Up Parsing. An Introductory Example
Bottom-Up Parsing Bottom-up parsing is more general than top-down parsing Just as efficient Builds on ideas in top-down parsing Bottom-up is the preferred method in practice Reading: Section 4.5 An Introductory
More informationApproximating Context-Free Grammars for Parsing and Verification
Approximating Context-Free Grammars for Parsing and Verification Sylvain Schmitz LORIA, INRIA Nancy - Grand Est October 18, 2007 datatype a option = NONE SOME of a fun filter pred l = let fun filterp (x::r,
More informationLexical analysis FORMAL LANGUAGES AND COMPILERS. Floriano Scioscia. Formal Languages and Compilers A.Y. 2015/2016
Master s Degree Course in Computer Engineering Formal Languages FORMAL LANGUAGES AND COMPILERS Lexical analysis Floriano Scioscia 1 Introductive terminological distinction Lexical string or lexeme = meaningful
More informationCompiler Construction
Compiler Construction Niklaus Wirth This is a slightly revised version of the book published by Addison-Wesley in 1996 ISBN 0-201-40353-6 Zürich, February 2014 Preface This book has emerged from my lecture
More informationFormal Grammars and Languages
Formal Grammars and Languages Tao Jiang Department of Computer Science McMaster University Hamilton, Ontario L8S 4K1, Canada Bala Ravikumar Department of Computer Science University of Rhode Island Kingston,
More informationParsing Technology and its role in Legacy Modernization. A Metaware White Paper
Parsing Technology and its role in Legacy Modernization A Metaware White Paper 1 INTRODUCTION In the two last decades there has been an explosion of interest in software tools that can automate key tasks
More informationCompilers. Introduction to Compilers. Lecture 1. Spring term. Mick O Donnell: michael.odonnell@uam.es Alfonso Ortega: alfonso.ortega@uam.
Compilers Spring term Mick O Donnell: michael.odonnell@uam.es Alfonso Ortega: alfonso.ortega@uam.es Lecture 1 to Compilers 1 Topic 1: What is a Compiler? 3 What is a Compiler? A compiler is a computer
More informationOBTAINING PRACTICAL VARIANTS OF LL (k) AND LR (k) FOR k >1. A Thesis. Submitted to the Faculty. Purdue University.
OBTAINING PRACTICAL VARIANTS OF LL (k) AND LR (k) FOR k >1 BY SPLITTING THE ATOMIC k-tuple A Thesis Submitted to the Faculty of Purdue University by Terence John Parr In Partial Fulfillment of the Requirements
More informationAlgorithms and Data Structures
Algorithms and Data Structures Part 2: Data Structures PD Dr. rer. nat. habil. Ralf-Peter Mundani Computation in Engineering (CiE) Summer Term 2016 Overview general linked lists stacks queues trees 2 2
More information03 - Lexical Analysis
03 - Lexical Analysis First, let s see a simplified overview of the compilation process: source code file (sequence of char) Step 2: parsing (syntax analysis) arse Tree Step 1: scanning (lexical analysis)
More informationBottom-Up Syntax Analysis LR - metódy
Bottom-Up Syntax Analysis LR - metódy Ján Šturc Shift-reduce parsing LR methods (Left-to-right, Righ most derivation) SLR, Canonical LR, LALR Other special cases: Operator-precedence parsing Backward deterministic
More informationTuring Machines: An Introduction
CIT 596 Theory of Computation 1 We have seen several abstract models of computing devices: Deterministic Finite Automata, Nondeterministic Finite Automata, Nondeterministic Finite Automata with ɛ-transitions,
More informationGrammars and parsing with Java 1 Peter Sestoft, Department of Mathematics and Physics Royal Veterinary and Agricultural University, Denmark E-mail: sestoft@dina.kvl.dk Version 0.08, 1999-01-18 1 The rst
More informationFlex/Bison Tutorial. Aaron Myles Landwehr aron+ta@udel.edu CAPSL 2/17/2012
Flex/Bison Tutorial Aaron Myles Landwehr aron+ta@udel.edu 1 GENERAL COMPILER OVERVIEW 2 Compiler Overview Frontend Middle-end Backend Lexer / Scanner Parser Semantic Analyzer Optimizers Code Generator
More informationYacc: Yet Another Compiler-Compiler
Stephen C. Johnson ABSTRACT Computer program input generally has some structure in fact, every computer program that does input can be thought of as defining an input language which it accepts. An input
More informationA Programming Language Where the Syntax and Semantics Are Mutable at Runtime
DEPARTMENT OF COMPUTER SCIENCE A Programming Language Where the Syntax and Semantics Are Mutable at Runtime Christopher Graham Seaton A dissertation submitted to the University of Bristol in accordance
More informationSemantic Analysis: Types and Type Checking
Semantic Analysis Semantic Analysis: Types and Type Checking CS 471 October 10, 2007 Source code Lexical Analysis tokens Syntactic Analysis AST Semantic Analysis AST Intermediate Code Gen lexical errors
More informationCompiler Design July 2004
irst Prev Next Last CS432/CSL 728: Compiler Design July 2004 S. Arun-Kumar sak@cse.iitd.ernet.in Department of Computer Science and ngineering I. I.. Delhi, Hauz Khas, New Delhi 110 016. July 30, 2004
More informationHow to make the computer understand? Lecture 15: Putting it all together. Example (Output assembly code) Example (input program) Anatomy of a Computer
How to make the computer understand? Fall 2005 Lecture 15: Putting it all together From parsing to code generation Write a program using a programming language Microprocessors talk in assembly language
More informationScoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)
Semantic Analysis Scoping (Readings 7.1,7.4,7.6) Static Dynamic Parameter passing methods (7.5) Building symbol tables (7.6) How to use them to find multiply-declared and undeclared variables Type checking
More informationIntel Assembler. Project administration. Non-standard project. Project administration: Repository
Lecture 14 Project, Assembler and Exam Source code Compiler phases and program representations Frontend Lexical analysis (scanning) Backend Immediate code generation Today Project Emma Söderberg Revised
More informationCompiler Construction
Compiler Construction Lecture 1 - An Overview 2003 Robert M. Siegfried All rights reserved A few basic definitions Translate - v, a.to turn into one s own language or another. b. to transform or turn from
More informationThe previous chapter provided a definition of the semantics of a programming
Chapter 7 TRANSLATIONAL SEMANTICS The previous chapter provided a definition of the semantics of a programming language in terms of the programming language itself. The primary example was based on a Lisp
More informationC Compiler Targeting the Java Virtual Machine
C Compiler Targeting the Java Virtual Machine Jack Pien Senior Honors Thesis (Advisor: Javed A. Aslam) Dartmouth College Computer Science Technical Report PCS-TR98-334 May 30, 1998 Abstract One of the
More informationThe International Journal of Digital Curation Volume 7, Issue 1 2012
doi:10.2218/ijdc.v7i1.217 Grammar-Based Specification 95 Grammar-Based Specification and Parsing of Binary File Formats William Underwood, Principal Research Scientist, Georgia Tech Research Institute
More informationHonors Class (Foundations of) Informatics. Tom Verhoeff. Department of Mathematics & Computer Science Software Engineering & Technology
Honors Class (Foundations of) Informatics Tom Verhoeff Department of Mathematics & Computer Science Software Engineering & Technology www.win.tue.nl/~wstomv/edu/hci c 2011, T. Verhoeff @ TUE.NL 1/20 Information
More informationLecture 9. Semantic Analysis Scoping and Symbol Table
Lecture 9. Semantic Analysis Scoping and Symbol Table Wei Le 2015.10 Outline Semantic analysis Scoping The Role of Symbol Table Implementing a Symbol Table Semantic Analysis Parser builds abstract syntax
More informationPushdown Automata. place the input head on the leftmost input symbol. while symbol read = b and pile contains discs advance head remove disc from pile
Pushdown Automata In the last section we found that restricting the computational power of computing devices produced solvable decision problems for the class of sets accepted by finite automata. But along
More information5HFDOO &RPSLOHU 6WUXFWXUH
6FDQQLQJ 2XWOLQH 2. Scanning The basics Ad-hoc scanning FSM based techniques A Lexical Analysis tool - Lex (a scanner generator) 5HFDOO &RPSLOHU 6WUXFWXUH 6RXUFH &RGH /H[LFDO $QDO\VLV6FDQQLQJ 6\QWD[ $QDO\VLV3DUVLQJ
More informationAutomata and Computability. Solutions to Exercises
Automata and Computability Solutions to Exercises Fall 25 Alexis Maciel Department of Computer Science Clarkson University Copyright c 25 Alexis Maciel ii Contents Preface vii Introduction 2 Finite Automata
More informationEfficient String Matching and Easy Bottom-Up Parsing
Efficient String Matching and Easy Bottom-Up Parsing (A DRAFT, submitted to CIAA 2007) Ján Šturc FMPhI CU Bratislava, jan.sturc@dcs.fmph.uniba.sk Abstract. The paper consists of the two parts. In the first
More informationChapter 5 Parser Combinators
Part II Chapter 5 Parser Combinators 5.1 The type of parsers 5.2 Elementary parsers 5.3 Grammars 5.4 Parser combinators 5.5 Parser transformers 5.6 Matching parentheses 5.7 More parser combinators 5.8
More informationFast nondeterministic recognition of context-free languages using two queues
Fast nondeterministic recognition of context-free languages using two queues Burton Rosenberg University of Miami Abstract We show how to accept a context-free language nondeterministically in O( n log
More informationChapter 7: Functional Programming Languages
Chapter 7: Functional Programming Languages Aarne Ranta Slides for the book Implementing Programming Languages. An Introduction to Compilers and Interpreters, College Publications, 2012. Fun: a language
More informationCS101 Lecture 11: Number Systems and Binary Numbers. Aaron Stevens 14 February 2011
CS101 Lecture 11: Number Systems and Binary Numbers Aaron Stevens 14 February 2011 1 2 1 3!!! MATH WARNING!!! TODAY S LECTURE CONTAINS TRACE AMOUNTS OF ARITHMETIC AND ALGEBRA PLEASE BE ADVISED THAT CALCULTORS
More informationMassachusetts Institute of Technology Department of Electrical Engineering and Computer Science
Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Fall 2005 Handout 7 Scanner Parser Project Wednesday, September 7 DUE: Wednesday, September 21 This
More informationParsing Expression Grammar as a Primitive Recursive-Descent Parser with Backtracking
Parsing Expression Grammar as a Primitive Recursive-Descent Parser with Backtracking Roman R. Redziejowski Abstract Two recent developments in the field of formal languages are Parsing Expression Grammar
More informationScanner. tokens scanner parser IR. source code. errors
Scanner source code tokens scanner parser IR errors maps characters into tokens the basic unit of syntax x = x + y; becomes = + ; character string value for a token is a lexeme
More informationANTLR - Introduction. Overview of Lecture 4. ANTLR Introduction (1) ANTLR Introduction (2) Introduction
Overview of Lecture 4 www.antlr.org (ANother Tool for Language Recognition) Introduction VB HC4 Vertalerbouw HC4 http://fmt.cs.utwente.nl/courses/vertalerbouw/! 3.x by Example! Calc a simple calculator
More informationChapter 2: Elements of Java
Chapter 2: Elements of Java Basic components of a Java program Primitive data types Arithmetic expressions Type casting. The String type (introduction) Basic I/O statements Importing packages. 1 Introduction
More informationOutline of today s lecture
Outline of today s lecture Generative grammar Simple context free grammars Probabilistic CFGs Formalism power requirements Parsing Modelling syntactic structure of phrases and sentences. Why is it useful?
More information3515ICT Theory of Computation Turing Machines
Griffith University 3515ICT Theory of Computation Turing Machines (Based loosely on slides by Harald Søndergaard of The University of Melbourne) 9-0 Overview Turing machines: a general model of computation
More informationNFAs with Tagged Transitions, their Conversion to Deterministic Automata and Application to Regular Expressions
NFAs with Tagged Transitions, their Conversion to Deterministic Automata and Application to Regular Expressions Ville Laurikari Helsinki University of Technology Laboratory of Computer Science PL 9700,
More informationProgramming Languages
Programming Languages Programming languages bridge the gap between people and machines; for that matter, they also bridge the gap among people who would like to share algorithms in a way that immediately
More informationBasic Parsing Algorithms Chart Parsing
Basic Parsing Algorithms Chart Parsing Seminar Recent Advances in Parsing Technology WS 2011/2012 Anna Schmidt Talk Outline Chart Parsing Basics Chart Parsing Algorithms Earley Algorithm CKY Algorithm
More informationFIRST AND FOLLOW Example
FIRST AND FOLLOW Example CS 447 -- Stephen Watt University of Western Ontario Copyright 2007 Stephen M. Watt FIRST/FOLLOW Main Ideas Look at all the grammar rules. Examine the possibilities of all substrings
More informationFactoring Surface Syntactic Structures
MTT 2003, Paris, 16 18 jui003 Factoring Surface Syntactic Structures Alexis Nasr LATTICE-CNRS (UMR 8094) Université Paris 7 alexis.nasr@linguist.jussieu.fr Mots-clefs Keywords Syntaxe de surface, représentation
More informationAnatomy of Programming Languages. William R. Cook
Anatomy of Programming Languages William R. Cook Copyright (C) 2013 2 Chapter 1 Preliminaries Preface What? This document is a series of notes about programming languages, originally written for students
More informationAdaptive LL(*) Parsing: The Power of Dynamic Analysis
Adaptive LL(*) Parsing: The Power of Dynamic Analysis Terence Parr University of San Francisco parrt@cs.usfca.edu Sam Harwell University of Texas at Austin samharwell@utexas.edu Kathleen Fisher Tufts University
More informationThe following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).
CHAPTER 5 The Tree Data Model There are many situations in which information has a hierarchical or nested structure like that found in family trees or organization charts. The abstraction that models hierarchical
More informationLearning Translation Rules from Bilingual English Filipino Corpus
Proceedings of PACLIC 19, the 19 th Asia-Pacific Conference on Language, Information and Computation. Learning Translation s from Bilingual English Filipino Corpus Michelle Wendy Tan, Raymond Joseph Ang,
More informationLanguage provides a means of communication by sound and written
Chapter 1 SPECIFYING SYNTAX 1 Language provides a means of communication by sound and written symbols. Human beings learn language as a consequence of their life experiences, but in linguistics the science
More information6.045: Automata, Computability, and Complexity Or, Great Ideas in Theoretical Computer Science Spring, 2010. Class 4 Nancy Lynch
6.045: Automata, Computability, and Complexity Or, Great Ideas in Theoretical Computer Science Spring, 2010 Class 4 Nancy Lynch Today Two more models of computation: Nondeterministic Finite Automata (NFAs)
More informationLecture 2: Regular Languages [Fa 14]
Caveat lector: This is the first edition of this lecture note. Please send bug reports and suggestions to jeffe@illinois.edu. But the Lord came down to see the city and the tower the people were building.
More informationComputer Science 281 Binary and Hexadecimal Review
Computer Science 281 Binary and Hexadecimal Review 1 The Binary Number System Computers store everything, both instructions and data, by using many, many transistors, each of which can be in one of two
More information7. Building Compilers with Coco/R. 7.1 Overview 7.2 Scanner Specification 7.3 Parser Specification 7.4 Error Handling 7.5 LL(1) Conflicts 7.
7. Building Compilers with Coco/R 7.1 Overview 7.2 Scanner Specification 7.3 Parser Specification 7.4 Error Handling 7.5 LL(1) Conflicts 7.6 Example 1 Coco/R - Compiler Compiler / Recursive Descent Generates
More informationHow To Understand A Sentence In A Syntactic Analysis
AN AUGMENTED STATE TRANSITION NETWORK ANALYSIS PROCEDURE Daniel G. Bobrow Bolt, Beranek and Newman, Inc. Cambridge, Massachusetts Bruce Eraser Language Research Foundation Cambridge, Massachusetts Summary
More information1 Introduction. 2 An Interpreter. 2.1 Handling Source Code
1 Introduction The purpose of this assignment is to write an interpreter for a small subset of the Lisp programming language. The interpreter should be able to perform simple arithmetic and comparisons
More informationIntroduction to Automata Theory. Reading: Chapter 1
Introduction to Automata Theory Reading: Chapter 1 1 What is Automata Theory? Study of abstract computing devices, or machines Automaton = an abstract computing device Note: A device need not even be a
More informationML for the Working Programmer
ML for the Working Programmer 2nd edition Lawrence C. Paulson University of Cambridge CAMBRIDGE UNIVERSITY PRESS CONTENTS Preface to the Second Edition Preface xiii xv 1 Standard ML 1 Functional Programming
More informationConstraints in Phrase Structure Grammar
Constraints in Phrase Structure Grammar Phrase Structure Grammar no movement, no transformations, context-free rules X/Y = X is a category which dominates a missing category Y Let G be the set of basic
More informationAutomata and Formal Languages
Automata and Formal Languages Winter 2009-2010 Yacov Hel-Or 1 What this course is all about This course is about mathematical models of computation We ll study different machine models (finite automata,
More informationTextual Modeling Languages
Textual Modeling Languages Slides 4-31 and 38-40 of this lecture are reused from the Model Engineering course at TU Vienna with the kind permission of Prof. Gerti Kappel (head of the Business Informatics
More informationUdacity cs101: Building a Search Engine. Extracting a Link
Udacity cs101: Building a Search Engine Unit 1: How to get started: your first program Extracting a Link Introducing the Web Crawler (Video: Web Crawler)... 2 Quiz (Video: First Quiz)...2 Programming (Video:
More informationA Programming Language for Mechanical Translation Victor H. Yngve, Massachusetts Institute of Technology, Cambridge, Massachusetts
[Mechanical Translation, vol.5, no.1, July 1958; pp. 25-41] A Programming Language for Mechanical Translation Victor H. Yngve, Massachusetts Institute of Technology, Cambridge, Massachusetts A notational
More informationBasics of Compiler Design
Basics of Compiler Design Anniversary edition Torben Ægidius Mogensen DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF COPENHAGEN Published through lulu.com. c Torben Ægidius Mogensen 2000 2010 torbenm@diku.dk
More informationNatural Language Database Interface for the Community Based Monitoring System *
Natural Language Database Interface for the Community Based Monitoring System * Krissanne Kaye Garcia, Ma. Angelica Lumain, Jose Antonio Wong, Jhovee Gerard Yap, Charibeth Cheng De La Salle University
More informationSemester Review. CSC 301, Fall 2015
Semester Review CSC 301, Fall 2015 Programming Language Classes There are many different programming language classes, but four classes or paradigms stand out:! Imperative Languages! assignment and iteration!
More informationCSC4510 AUTOMATA 2.1 Finite Automata: Examples and D efinitions Definitions
CSC45 AUTOMATA 2. Finite Automata: Examples and Definitions Finite Automata: Examples and Definitions A finite automaton is a simple type of computer. Itsoutputislimitedto yes to or no. It has very primitive
More informationSymbiosis of Evolutionary Techniques and Statistical Natural Language Processing
1 Symbiosis of Evolutionary Techniques and Statistical Natural Language Processing Lourdes Araujo Dpto. Sistemas Informáticos y Programación, Univ. Complutense, Madrid 28040, SPAIN (email: lurdes@sip.ucm.es)
More informationThe Halting Problem is Undecidable
185 Corollary G = { M, w w L(M) } is not Turing-recognizable. Proof. = ERR, where ERR is the easy to decide language: ERR = { x { 0, 1 }* x does not have a prefix that is a valid code for a Turing machine
More informationIntroduction to formal semantics -
Introduction to formal semantics - Introduction to formal semantics 1 / 25 structure Motivation - Philosophy paradox antinomy division in object und Meta language Semiotics syntax semantics Pragmatics
More informationStatic vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer
Lecture 10: Static Semantics Overview 1 Lexical analysis Produces tokens Detects & eliminates illegal tokens Parsing Produces trees Detects & eliminates ill-formed parse trees Static semantic analysis
More information[Refer Slide Time: 05:10]
Principles of Programming Languages Prof: S. Arun Kumar Department of Computer Science and Engineering Indian Institute of Technology Delhi Lecture no 7 Lecture Title: Syntactic Classes Welcome to lecture
More informationCompilers Lexical Analysis
Compilers Lexical Analysis SITE : http://www.info.univ-tours.fr/ mirian/ TLC - Mírian Halfeld-Ferrari p. 1/3 The Role of the Lexical Analyzer The first phase of a compiler. Lexical analysis : process of
More informationLexical Analysis and Scanning. Honors Compilers Feb 5 th 2001 Robert Dewar
Lexical Analysis and Scanning Honors Compilers Feb 5 th 2001 Robert Dewar The Input Read string input Might be sequence of characters (Unix) Might be sequence of lines (VMS) Character set ASCII ISO Latin-1
More informationSOLUTION Trial Test Grammar & Parsing Deficiency Course for the Master in Software Technology Programme Utrecht University
SOLUTION Trial Test Grammar & Parsing Deficiency Course for the Master in Software Technology Programme Utrecht University Year 2004/2005 1. (a) LM is a language that consists of sentences of L continued
More informationMassachusetts Institute of Technology Department of Electrical Engineering and Computer Science
Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2010 Handout Semantic Analysis Project Tuesday, Feb 16 DUE: Monday, Mar 1 Extend your compiler
More informationThe Cool Reference Manual
The Cool Reference Manual Contents 1 Introduction 3 2 Getting Started 3 3 Classes 4 3.1 Features.............................................. 4 3.2 Inheritance............................................
More informationSOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGE
SOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGE Amal M R, Jamsheedh C V and Linda Sara Mathew Department of Computer Science and Engineering, M.A College of Engineering, Kothamangalam,
More informationCA4003 - Compiler Construction
CA4003 - Compiler Construction David Sinclair Overview This module will cover the compilation process, reading and parsing a structured language, storing it in an appropriate data structure, analysing
More informationProgramming Language Pragmatics
Programming Language Pragmatics THIRD EDITION Michael L. Scott Department of Computer Science University of Rochester ^ШШШШШ AMSTERDAM BOSTON HEIDELBERG LONDON, '-*i» ЩЛ< ^ ' m H NEW YORK «OXFORD «PARIS»SAN
More informationUniversity of Wales Swansea. Department of Computer Science. Compilers. Course notes for module CS 218
University of Wales Swansea Department of Computer Science Compilers Course notes for module CS 218 Dr. Matt Poole 2002, edited by Mr. Christopher Whyley, 2nd Semester 2006/2007 www-compsci.swan.ac.uk/~cschris/compilers
More informationInterpreting areading Scaled Scores for Instruction
Interpreting areading Scaled Scores for Instruction Individual scaled scores do not have natural meaning associated to them. The descriptions below provide information for how each scaled score range should
More information