Parser
Parser
Top-down methods:
• Parse-trees built from root to leaves.
• Input to parser scanned from left to right one symbol at a time
The Parser
Bottom-up methods:
• Start from leaves and work their way up to the root.
• Input to parser scanned from left to right one symbol at a time
Dealing With Errors
If compiler had to process only correct programs, its
design and implementation would be simplified greatly!
Terminals Nonterminals
(token name)
Example:
Start Productions
Symbol
Derivations
• Starting with start symbol
• At each step: a nonterminal replaced
with the body of a production
Example:
Example:
( id )$
+ε )$
( id +)$
*ε +)$
( id *+)$
LL(1) Grammars
• For recursive-descent parsers with no
backtracking
• L = scan from left to right
• L = left-most derivation
• 1 symbol lookahead
• Cannot be left-recursive or ambiguous
• If A-> F | T
– FIRST(F) and FIRST(T) are disjoint
– if ε is in FIRST(T) then FIRST(F) and FOLLOW(A)
are disjoint … and likewise when ε is in FIRST(F)
Parsing Table
Parsing Table
• Two dimensional array
– Rows: nonterminals Columns: input symbols
• M[A,a] where A is nonterminal and a is terminal
or $
• Gives the production rule to use.
First Follow
( id )$
+ε )$
( id +)$
*ε +)$
( id *+)$
Exercise
For the following productions:
S-> +SS | * SS | a