This switch statement is working with one Case how to make it multiple case Bison

北城以北 提交于 2019-12-13 04:35:02

问题


This switch statement is working with one Case how to make it multiple case

S: Statement {printf("ACCEPTED");}

Statement:SWITCH BRO ID BRC CBO E CBC

E: A 
 | A C




A: A B 
 | CASE DIGIT D 


B:  BREAK SEMI


C : DEFAULT D B



D : COLON ID SEMI

 ;

This is an issue i want to make grammar accepted with multiple cases


回答1:


Your grammar is terrible, and does not account for repetition of the CASE. You need to discover how to express lists of things. The general pattern is like:

item: ....
items: item
       | item ';' items

So we have:

case: CASE DIGIT ':' stmts
default: DEFAULT ':' stmts
cases: case | default | case ';' cases

You should also find out how you can trace the parser for debugging. With yacc, you can set the environment variable YYDEBUG and it will print a quite verbose of all parser states.




回答2:


Correct switch grammar Don't need to use semicolon in between A E space will work.

Statement:SWITCH BRO ID BRC CBO E CBC

E: A | A C | A E

A: A B | CASE DIGIT D

B: BREAK SEMI

C : DEFAULT D B

D : COLON ID SEMI



来源:https://stackoverflow.com/questions/21073980/this-switch-statement-is-working-with-one-case-how-to-make-it-multiple-case-biso

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!