CS 614: Theory and Construction of Compilers Lecture 4 Fall 2002 Department of Computer Science University of Alabama Joel Jones
©2002 Joel Jones Overview Extended BNF Grammar Transformations Lexical Scanners Recursive Descent Parsers
©2002 Joel Jones Extended BNF Combination of BNF and REs Has form N ::= X, where N is a non- terminal and X is an extended RE extended RE – RE constructed from both terminal and non-terminal symbols Right-hand-side can contain ‘*’, ‘(‘, and ‘)’
©2002 Joel Jones Grammar Transformations Left factorization Example: given X Y | X Z, Replace with X ( Y | Z) Left Recursion Elimination Example: given N ::= X | N Y Replace with N ::= X (Y)* Pair Up: Transform: Identifier ::= Letter | Identifier Letter | Identifier Digit
©2002 Joel Jones Systematic development of scanners Express the lexical grammar in EBNF, performing any necessary grammar transformations Transcribe each EBNF production rule N ::= X to a scanning method scanN whose body is determined by X (cont.)
©2002 Joel Jones Systematic development of scanners (cont.) Make the scanner consist of A private variable currentChar Private auxilary methods take and takeIt Private scanning methods from above, enhanced to record token’s kind and spelling A public scan method that scans “Seperator* Token” discarding any seperators but returning the token that follow them
©2002 Joel Jones Systematic development of a recursive-descent parser Express the grammar in EBNF, with a single production rule for each nonterminal symbol, and perform any necessary grammar transformations. Eliminate left recursion and left factorize wherever possible Transcribe each EBNF production rule N ::= X to a parsing method parseN, whose body is determined by X
©2002 Joel Jones Systematic development of a recursive-descent parser Make the parser consist of: A private variable currentToken Private parsing methods developed above Private auxiliary methods accept and acceptIt both of which call the scanner A public parse method that calls parseS, where S is the start symbol of the grammar, having first called the scanner to store the first input token in currentToken
©2002 Joel Jones Try It! Transform the grammar from the handout