Search code examples
parsingcompiler-constructionantlr

ANTLR error on parsing


i have an problem with antlr. I have the following simple grammar:

grammar bxk; 
options
{
    language=CSharp3;
}

// start rule
start
    : root* EOF
    ;


root
    : ROOT_ID CT_ID ((rd=rootDefinition) | END)
    ;


rootDefinition  
    : '{'
      ('scope' ASSIGN s=STRING END)?
      ('major' ASSIGN i=STRING END)?
      ('minor' ASSIGN i=STRING END)?
      ('revision' ASSIGN i=STRING END)?
      '}'
    ;   

CHAR    :  ('a'..'z'|'A'..'Z') ;
ROOT_ID :  'ROOT_'(CHAR | DIGIT | SPECIAL)+ ;
CT_ID   :  'ct_'(CHAR | DIGIT | SPECIAL)+ ;
DIGIT   :  '0'..'9';
SPECIAL :  '_' ;
END :  ';';
STRING  :  CHAR (CHAR | DIGIT | SPECIAL)*;
WS      :  (' '|'\t' | '\n' | '\r' | '\u000C')+ {Skip();} ; 

That is all. Now when i generate the c# code i have several errors: The funktion 'start' is private and i must change always to public. Furthermore when i change to public and will parse the follwing:

ROOT_base ct_s { 
 scope=aliejfoac;
}

Internaly the NoViableAltException occurs. If i add an space after the semicolon it runs properly. But i can only see the exception in Debbug mode with the Visual Studio.

Another problem i have ist that syntay errors don't display. I have add:

catch [RecognitionException re] {
    ReportError(re);
    throw new Exception(re.ToString() + "\non line " + re.Line + " and row " + re.CharPositionInLine.ToString());
}

Now an exception will be displayed if an syntax error occurs.

Finally i have the problem with the Skip(). When i write it Skip() it works with the generated code and not with the interpreter. When i write skip() it works only with the interpreter and not with the generated code.

Can anyone help me with my problems?


Solution

  • user1469116 wrote:

    The funktion 'start' is private and i must change always to public.

    All rules are by default private in the C# target. Explicitly make it public by adding the keyword public in front of your entry-point rule:

    public start
     : root* EOF
     ;
    

    user1469116 wrote:

    Internaly the NoViableAltException occurs. If i add an space after the semicolon it runs properly.

    If I add the rule:

    ASSIGN :  '=';
    

    I can successfully parse your input. ANTLRWorks' debugger parses it as follows:

    enter image description here

    user1469116 wrote:

    Finally i have the problem with the Skip(). When i write it Skip() it works with the generated code and not with the interpreter. When i write skip() it works only with the interpreter and not with the generated code.

    The interpreter (from ANTLRWorks, I assume) ignores everything inside the options-section of your grammar and is Java based. And the Java target only "knows" of a skip(), whereas the C# target only "knows" of a Skip() method.