0

I have this grammar:

agent
 = nil 
 | @ 
 | id 
 | act . agent
 | agent + agent
 | agent "|" agent
 | agent \ restriction
 |  agent [relabeling]
 | agent where agent_frame end
 | automation
 | (agent)

where the priorities are:

"where" < "+" < "|" < "\" < "." < "[" < "nil", "@"

I need to delete the left recursion respecting the priorities ( and write all in JavaCC).

Can you help me to delete recursion?

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
Domenico
  • 101
  • 7
  • your grammar is ugly. Can't you just indent grammar source by 4 spaces and replace it in your question? Thanks. – Seki Jul 10 '15 at 12:50

2 Answers2

2

Dinesh thank you for the answer, your solution give me a conflict in JavaCC with (agent-postfix)*. I solved in this way:

agent=agent2 agent'
agent'= "where" agent_frame "end" agent' | epsilon

agent2= agent3 agent2'
agent2'= "+" agent3 agent2' | epsilon

agent3= agent4 agent3'
agent3'= "|" agent4 agent3' | epsilon

agent4 = agent5 agent4'
agent4'= "\" restriction agent4' | epsilon

agent5: act "." agent | agent6

agent6 = agent7 agent6'
agent6'= "[" relabeling "]" agent6' | epsilon

agent7= id | automaton | "(" agent ")" | "nil" | "@"

but I don't know if this solution is correct.

Thank you very much.

Regards Domenico

Theodore Norvell
  • 15,366
  • 6
  • 31
  • 45
Domenico
  • 101
  • 7
  • This is correct in that you have eliminated the left recursion and kept the language the same. But, the grammar is still ambiguous. For example there are two leftmost derivations from `agent3` to `act . id | id`. I haven't tried it, but I expect that JavaCC will give a warning for this grammar. I think the best way to fix it is to change the rule for `agent5` to `agent5 = act . agent5`. This eliminates the leftmost derivation that goes `a3 =>* a4 =>* a5 =>* act . a =>* act . id | id"`, but leaves one that goes `a3 =>* a4 | a4 =>* act . id | id`. – Theodore Norvell Jul 11 '15 at 06:38
  • Hi Theodore Norvell, thanks for your answer. I solved the problem with agent5 using a LOOKAHEAD 2 in JavaCC. So I have "agent5 = lookahead(2) act . agent | agent6". Acconding to you this change is correct? Thank you very much. Regards Domenico – Domenico Jul 11 '15 at 08:35
  • I don't know what "act" is. Is it a nonterminal? Does `act =>* id`? If the answer to both these questions is "yes", then there is a choice conflict between `act . agent` and `agent6`, furthermore your `LOOKAHEAD(2)` looks like a correct way to resolve this choice conflict. (I probably would have written `LOOKAHEAD( act . )`. – Theodore Norvell Jul 11 '15 at 16:50
  • However, that is not the problem that I was pointing out in my comment. The grammar in your answer is ambiguous. Furthermore JavaCC will resolve the ambiguity in a way that you don't want. For example, your grammar allows this parse tree http://tinyurl.com/o2al54v (I assumed that there is a production rule `act = id`.) – Theodore Norvell Jul 11 '15 at 17:50
  • Theodore Norvell, I need of a grammar without left recursion (keeping the priorities) and no ambiguous. Can you write it me? Because I don't know how to solve completely the problem. I am writing a parser using JavaCC for a university project. Thank you very much. Regards Domenico – Domenico Jul 11 '15 at 18:49
  • Can you let me know the definition of `act`? – Theodore Norvell Jul 11 '15 at 21:50
  • Also, what is the definition of automaton? – Theodore Norvell Jul 11 '15 at 22:01
  • Theodore Norvell this is my complete grammar: [link] (https://www.dropbox.com/s/my1ggf9n8rmu44v/ccs.pdf?dl=0). – Domenico Jul 12 '15 at 07:23
  • To answer your earlier question: No I won't write your grammar for you. You've almost completely solve this by yourself. There are only two modifications you need to make to your answer, as it stands, correct. The first you already know. JavaCC's default lookahead of 1 is not enough to decide the two choices for `agent5`. You can use either `LOOKAHEAD(2)` or `LOOKAHEAD( act() ".")`; either will work. Second there is the ambiguity I noted above. After a "." you want an agent5. So change the rule to `agent5 = act "." agent5 | agent6` and you are done. – Theodore Norvell Jul 12 '15 at 20:00
  • Theodore Norvell thank you very much for your help, thanks to your tips everything works correctly. Regards Domenico – Domenico Jul 13 '15 at 17:22
0

I'm no JavaCC expert, but here is how you can get started to get rid of your left recursion:

agent-primary
 = nil
 | @
 | id
 | act . agent
 | automation
 | (agent)

agent-postfix
 = + agent
 | "|" agent
 | \ restriction
 | [relabeling]
 | where agent_frame end

agent
 = agent-primary (agent-postfix)* 

You might face some conflicts with the right agent calls as well in the "binary" expressions such as agent + agent.

In any case, your grammar looks very similar to arithmetic expressions, so I advice you to have a look at how these are typically handled in JavaCC.

Dinesh Bolkensteyn
  • 2,971
  • 1
  • 17
  • 20