Download presentation
Presentation is loading. Please wait.
Published byRuby Lang Modified over 9 years ago
1
PZ03BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 1 PZ03BX –Recursive descent parsing Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 3.4
2
PZ03BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 2 Parsing - Parsing is the process of tracing or constructing a parse tree for a given input string - Parsers operate on the non-terminals obtained by the lexical analyzer which breaks the inpur stream into tokens. - A recursive descent parser traces out a parse tree in top-down order; it is a top-down parser - Each nonterminal in the grammar has a subprogram associated with it; the subprogram parses all sentential forms that the nonterminal can generate - The recursive descent parsing subprograms are built directly from the grammar rules - The recursive descent parser relies on the fact that the language is LR(k)
3
PZ03BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 3 Recursive descent parsing overview A simple parsing algorithm Shows the relationship between the formal description of a programming language and the ability to generate executable code for programs in the language. Use extended BNF for a grammar, e.g., expressions: ::= {[+|-] }* Consider the recursive procedure to recognize this: procedure Expression; begin Term; /* Call Term to find first term */ while ((nextchar=`+') or (nextchar=`-')) do begin nextchar:=getchar; /* Skip over operator */ Term end
4
PZ03BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 4 Generating code Assume each procedure outputs its own postfix (Section 8.2, to be discussed later) To generate code, need to output symbols at appropriate places in procedure. procedure Expression; begin Term; /* Call Term to find first term */ while ((nextchar=`+') or (nextchar=`-')) do begin nextchar:=getchar; /* Skip over operator */ Term; output previous ‘+’ or ‘-’; end
5
PZ03BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 5 Generating code (continued) Each non-terminal of grammar becomes a procedure. Each procedure outputs its own postfix. Examples: procedure Term; begin Primary; while ((nextchar=`*') or (nextchar=`/')) do begin nextchar:=getchar; /* Skip over operator */ Primary; output previous ‘*’ or ‘/’; end Procedure Identifier; begin if nextchar= letter output letter else error; nextchar=getchar; end Figure 3.13 of text has complete parser for expressions.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.