0

I am trying to write a C grammar with Antlwork, and for that I used this one http://stuff.mit.edu/afs/athena/software/antlr_v3.2/examples-v3/java/C/C.g where I tried to make it more simple by removing many blocks I don't use and the backtracking. And here is what I have : http://www.archive-host.com/files/1956778/24fe084677d7655eb57ba66e1864081450017dd9/CNew.txt

Then when I do ctrl+D, I get a lot of warning and errors like these:

[21:20:54] warning(200): C:\CNew.g:188:2:  Decision can match input such as "'{' '}'" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
[21:20:54] warning(200): C:\CNew.g:210:2: Decision can match input such as "'for' '('" using multiple alternatives: 2, 3
As a result, alternative(s) 3 were disabled for that input
[21:20:54] error(201): C:\CNew.g:210:2: The following alternatives can never be matched: 3

[21:20:54] error(208): C:\CNew.g:250:1: The following token definitions can never be matched because prior tokens match the same input: CHAR

I don't really understand why I have all these warnings, there should not be conflicts.

Exia0890
  • 441
  • 6
  • 21
  • Then don't remove the `backtrack` (and `memoize`) options. – Bart Kiers Mar 06 '13 at 20:46
  • thanks for the reply, but a friend told me that using backtrack would cause me later many difficulties when I will try to do the AST. – Exia0890 Mar 06 '13 at 20:50
  • 2
    You have 2 options here: `1)` either rewrite the grammar so that the ambiguities are no longer there (which is probably quite hard, seeing you started with a grammar that had backtracking enabled), or `2)` re-enable backtracking. Ask your friend what exactly s/he meant by *"many difficulties when I will try to do the AST"*, because it sounds like non-sense. There's no problem creating an AST from a parser grammar that has backtracking enabled. – Bart Kiers Mar 06 '13 at 20:58
  • well when I did the grammar, I modified a lot of blocks so that it would fit even without the backtrack, or that is what I thought but since I am beginner, I must have missed many things. As for the backtrack he told me that it would create a lot of nodes wich may burden a lot the parser and the compilator wich i will try to write with this grammar. But it is true that with the backtrack I don't have any warnings anymore, but I still have this error – Exia0890 Mar 06 '13 at 21:18
  • [22:02:55] error(208): C:\Users\Seiya\Desktop\projets\TRAD\Gram\CNew.g:238:1: The following token definitions can never be matched because prior tokens match the same input: CONSTANT [22:17:18] error(208): CNew.g:251:1: The following token definitions can never be matched because prior tokens match the same input: CHAR [22:17:18] error(208): C:\Users\Seiya\Desktop\projets\TRAD\Gram\CNew.g:251:1: The following token definitions can never be matched because prior tokens match the same input: CHAR – Exia0890 Mar 06 '13 at 21:18
  • 1
    Having a lot of nodes shouldn't burden the parser, I wouldn't think. – Kyle Strand Mar 06 '13 at 21:22
  • 2
    @KyleStrand, the generated AST would not contain any more nodes whether backtracking is enabled or disabled. – Bart Kiers Mar 06 '13 at 21:24
  • I see, thanks for the clarifications, I will try to do with the backtrack. – Exia0890 Mar 06 '13 at 21:46

1 Answers1

1

but I still have this error

[22:02:55] error(208): C:\Users\Seiya\Desktop\projets\TRAD\Gram\CNew.g:238:1: The following token definitions can never be matched because prior tokens match the same input: CONSTANT [22:17:18] error(208): CNew.g:251:1: The following token definitions can never be matched because prior tokens match the same input: CHAR [22:17:18] error(208): C:\Users\Seiya\Desktop\projets\TRAD\Gram\CNew.g:251:1: The following token definitions can never be matched because prior tokens match the same input: CHAR

That means the lexer can never create the tokens CHAR and INT because some other lexer rule, CONSTANT, matches the same input. What you need to do is change CONSTANT into a parser rule.

In other words, change these two rules:

primary_expression
    : ID
    | CONSTANT
    | '(' expression ')'
    ;

CONSTANT
    :   INT
    |   CHAR
    ;

into the following:

primary_expression
    : ID
    | constant
    | '(' expression ')'
    ;

constant
    :   INT
    |   CHAR
    ;
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • Thanks, but does that mean that in a lexer rule definition you can't use another one ? here is what i have http://i.imgur.com/7EnFgRu.jpg At the left I have a red subtree I suppose it means there is ambiguities . And just below the : (external_declaration)+ line, the ";" caractere is in red . Is it bad ? – Exia0890 Mar 06 '13 at 21:48
  • 1
    @Exia0890, yes, a lexer rule can use another lexer rule. But how you set it up, the lexer could never have created `INT` and `CHAR` tokens since a `CONSTANT` **always** would have been created. You either had to make `INT` and `CHAR` fragment-rules, or "promote" `CONSTANT` to a parser rule (the latter being more appropriate, hence my recommendation). See: http://stackoverflow.com/questions/6487593/what-does-fragment-means-in-antlr – Bart Kiers Mar 06 '13 at 21:55
  • The red in your image is a (partial) parse that the parser has given up on since it could not complete said parse. The parser backtracked from the `function_definition` rule to match the `declaration` rule. – Bart Kiers Mar 06 '13 at 21:57
  • I see, thanks, I did not know about the fragment-rule it is pretty useful too. – Exia0890 Mar 06 '13 at 22:19