Re: [antlr-discussion] Antlr4 - Syntax error when parsing constant value

30 views
Skip to first unread message

David Whitten

unread,
Apr 17, 2015, 11:49:48 AM4/17/15
to antlr-di...@googlegroups.com
I assume that typeSpecifier resolves basically to an identifier. (since I don't see it in the grammar that you sent us)

your list of rules for primaryExpression has identifier as a higher priority than declaration so I expect it is trying to parse the wrong alternative.

Of course, if I am wrong, I would appreciate an explanation why.

David
713-870-3834

On Fri, Apr 17, 2015 at 9:26 AM, Dave Pearce <dave....@vr-tek.co.uk> wrote:

Hi

I'm trying to parse an ANSI C file and am having trouble setting up the rules for array variable declarations. I'm trying the rules out with the following C statements:
Line
  1       char    szString10;
  2
  3       char    szString1[];
  4
  5       char    szString1[2];
  6
  7       char    szString2[10];

My Grammar snippet is as follows:

grammar C;

cFile
    :    primaryExpression+
    ;

primaryExpression
    :    Identifier
    |   Constant
    |   StringLiteral+
    |   '(' expression ')'
    |    declaration
    |    preProcessorDirective
    |    blockComment
    |    lineComment
    |   genericSelection
    ;

declaration
    :    typeSpecifier Identifier '[' Dimension? ']') ';'
    |    typeSpecifier Identifier ';'
    ;

/* Lexer */

Dimension
    :    Digit+
    ;

fragment
Digit
    :   [0-9]
    ;

When I try to parse the C statements above, I get the following syntax error:

Lexer syntax error at 5:15 no viable alternative at input 'charszString1[2'

Stack:
Rule: 0 - cFile
Rule: 1 - primaryExpression

Line : char szString1[2];

Offending Symbol: [@10,56:56='2',<116>,5:15]

The parser has successfully parsed the statements on lines 1 and 3 but is objecting to the "Dimension" lexer rule.

Can anyone see what I'm doing wrong?

Thanks
Dave

--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages