开发者

Using first-set in recursive descent parser

开发者 https://www.devze.com 2023-04-05 07:35 出处:网络
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:

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 identifier
  • compound-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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号