Programming Language Syntax 6 http://flic.kr/p/zCyMp
Let’s pick up where we left off last time: Execute recursive-descent parser on each of these inputs Create a parse tree as you go Each time a procedure is called, hang a node off the calling procedure Be sure to keep track of the call stack
Solution
The tricky part: How to predict? … …
Given input_token, how do you predict the replacement for each rule?
A token X may predict a production P for either of two reasons: Right-hand side of P yields string that starts with X Right-hand side of P yields nothing (empty string) and X begins yield of whatever comes after P
More formally Given individual terminal/non-terminal B and concatenation of 1+ terminals/non-terminals A FIRST(A): Set of tokens that could start A FOLLOW(A): Set of tokens that could come after A PREDICT(BA ): Set of tokens that predicts that B will replaced with A We’ll also need: EPS(A): True if A could yield an empty string; false otherwise But how to compute these?
Which FIRST, FOLLOW, and EPS values can you deduce Which FIRST, FOLLOW, and EPS values can you deduce? Find some “obvious” ones
Here are some obvious ones
Given these, can you deduce more?
Given these, can you deduce more? How about this one Hint
Given these, can you deduce more? {id,read,write} subset of FIRST(stmt_list)
Given these, can you deduce more? How about this one Hint
Given these, can you deduce more? $$ in FIRST(program)
Algorithm for computing EPS values and FIRST sets
Activity: Run algorithm on grammar
What’s next? Finish activity for next class Homework 2 due on Tuesday