0

I am trying to reduce the grammar to LL(1) for a hypothetical language we created. I have removed most of the left factoring issues in the grammar, using the general rule of introducing new non-terminal characters for the same.

For example,

<assignmentstats>------- <type><id>=<E>/ id=<E>/id'['<E>']'=<E>/     

is transformed to

<assignmentstats>------- <type><id>=<E>/ id<LF3>
<LF3>------- =<E>/[<E>]=<E>

After applying such rules for all the required statements, I expected an unambiguous grammar.

But the following statement is somehow still ambiguous.

<body>------- <forrelatedstuff>/ <floors>/<stats> 

Here are the related statements of the grammar(only those required has been shown)

<funcbody>----- {<stats>}    
<stats>----- <stat> <stats>/ #       
<floors>-------- <floor><floors>/ #     
<floor>------- floo <id><arr>{stats}/id<arr>{<stats>}     
<stat>----- <superstats>/<returnstats>
<superstats>----- <type>id<Zip>/id<LF3>  
<building> ------- build <id> {<body>}   
<returnstats>--------- return <E>  

I looked more into it , and found that the ambiguity is between 'floors' and 'stats'. The FIRST('stats') and FIRST('floors'), i.e. the set of first terminal characters contain 'id' and '}' for both.

I can see why this would be a problem and how left factoring could solve this.But how can I remove such kind of ambiguity through left factoring?

Note: Here, 'id' denotes an identifier.

'#' denotes epsilon.

viv1
  • 99
  • 2
  • 10
  • If by "refactoring" you mean reshaping the code (er, BNF) with affecting its functionality, then no amount of refactoring will remove the ambiguity, because your grammar *defines* what is correct, including the ambiguity you don't like. You might edit the grammar arbitrarily to remove the ambiguity, but that would not be a refactoring. – Ira Baxter Feb 07 '15 at 16:22
  • @IraBaxter No.I mean using rules of the following nature. `A->cB|cK` should be transformed to `A->cX` `X->B|K` This would make the parser able to predict which direction it should go to, by looking ahead only one symbol(in this example, 'c'). – viv1 Feb 07 '15 at 18:37
  • What you want to do is to fix a *local* ambiguity. – Ira Baxter Feb 07 '15 at 23:35

0 Answers0