Using first-set in recursive descent parser
I'm writing a recursive descent parser for C in C++. I don't know how to choose the right production in the following case:
statement: labeled-statement | compound-statement | expression-statement | selection-statement | iteration-statement | jump-statement
I read about the "first"-set which compares the lookahead-token/char with possible terminals which comes first in the productions. Currently I'm stuck at using a first-set in a recursive descent parser, because I only have a function and nothing else, no object for each ru开发者_JAVA技巧le or anything else with which I can identify a rule/production.
Your grammar is invalid for recursive descent parsers because it's ambiguous on the left side:
labeled-statement
starts with an identifiercompound-statement
starts with a{
(this is fine)expression-statement
starts with an identifier or a number (or(
)
Can stop here, you have a clash between labeled statement and expression statement. You need to transform your grammer to get rid of left-side ambiguities (through temporary grammar nodes to contain the common parts so when you branch you can determine which branch to go to using only the look-ahead).
精彩评论