More on Parsers and Grammars
Topics
- Top down and bottom up parsers
- Bottom up parsers
- LL(k) and LR(k) parsers
- Limitations of BNF Grammars
- Attribute Grammars
- Creating parsers
- Knuth
Top Down and Bottom Up Parsers
- Recursive descent parsers are an example of what is called a
top down parsers because the sequence of procedure calls corresponds to the sequence of substutitions
that occur when going through the tree from the top down
- Another type of parser corresponds to the sequence of substitutions corresponding to substitutions
from the bottom up
- Top down parsers follow the sequence of substitutions of a leftmost derivation
- Bottom up parsers follow the the sequence of substitutions of a
rightmost derivation (but
the steps are generated in the reverse order of the derivation)
Classes of Grammars and Parsers
- For some grammars it's impossible to build a top down parser
- The class of grammars that have top down parsers are called LL(k)
[Left-to-right scan, Leftmost derivation, k symbol lookahead]
- The class of parsers that have bottom up parsers are called LR(k)
[Left-to-right scan, Rightmost derivation, k symbol lookahead]
- LR(1) > LL(1)
- For a grammar to be LL(1), we must be able to choose the next
production by looking at the next symbol
Limitations of Context Free Grammars
- Bottom Up parsers can parse any BNF (ie context free) Grammar
- Some common syntax rules can't be specified using BNF
- Declare before use
- End statement names: procedure foo ... end foo
- procedure calls match declaration: procedure x(i: integer) ... x(3)
Attribute Grammars
- To overcome limitations, we add attributes to the productions and
pass the attributes up and down the tree
- Example:
PROC -> procedure IDENTIFIER1 is PROCBODY end IDENTIFIER2;
RULEforPROC: IDENTIFIER1.string =
IDENTIFIER2.string
Creating Parsers
- Lexical analyzers and parsers can be created automatically or by
hand:
- Automatically
- Lex: Grammar -> Lexical Analyzer
- YACC: Grammar -> Parser
- BISON
- By hand: Recursive descent parser is easiest, if the grammar is
suitable
Develoment of Parsing Algorithms
- Earliest: all by hand; no underlying theory
- Later: top down, operator precedence; little theory
- Knuth:
- clarified top down algs
- invented bottom up alg
- Developed attribute grammars