GET-grammar rule - please help for understanding

25 views
Skip to first unread message

silvia.iva...@gmail.com

unread,
Mar 2, 2019, 6:36:08 PM3/2/19
to antlr-discussion
Hello, I have a antlr v4-grammar and I am trying to understand the GET-rule of this grammar, but I do not understand where I am wrong.I think I must use JSON instead of MAP but when I use JSON the program throws too many exceptions. 

Could you please help me? 

Here is the program, that I have generated.


PROGRAM aa
{
MAP m
;
m
= '{"aaaa":"17"}' ;
PRINT GET
(m , "aaaa")  ;
}




Here is the grammar for the language above.

grammar program;
prule
: PROGRAM VARIABLE block;


WS
: [ \t\r\n]+ -> skip;


NUMBER
: '-'? '0'..'9'+([.,]'0'..'9'+)?;
TEXT
: '"' .* '"';
JSON
: '\'' .* '\'';
OPEN_BLOCK
: '{';
CLOSE_BLOCK
: '}';
ASSIGNMENT
: '=';
MAPDECL
: 'MAP';
DOUBLEDECL
: 'DOUBLE';
TEXTDECL
: 'TEXT';
BOOLDECL
: 'BOOLEAN';
PRINT
: 'PRINT';
OPEN_BRACKET
: '(';
CLOSE_BRACKET
: ')';
COMMA
: ',';
PROGRAM
: 'PROGRAM';
SMALLER
: '<';
GREATER
: '>';
TRUE
: 'TRUE';
FALSE
: 'FALSE';
GET
: 'GET';
SEMICOLON
: ';';
VARIABLE
: ('a'..'z'|'A'..'Z')+;


block
: OPEN_BLOCK statement+ CLOSE_BLOCK;


statement
: (assignment |
            mapdecl
|
            booldecl
|
            doubledecl
|
            textdecl
|
           
print
           
) SEMICOLON;


mapdecl
: MAPDECL VARIABLE;
booldecl
: BOOLDECL VARIABLE;
doubledecl
: DOUBLEDECL VARIABLE;
textdecl
: TEXTDECL VARIABLE;


print: PRINT (expression | logexpression);


assignment
: VARIABLE ASSIGNMENT (expression | logexpression);


expression
: factor ((OPERATOR_ADD|OPERATOR_SUB) expression)?;


factor
: (NUMBER |
         TEXT
|
         TRUE
|
         FALSE
|
         JSON
|
         
get |
         VARIABLE
(OPEN_BRACKET (expression (COMMA expression)*)? CLOSE_BRACKET)? |
         OPEN_BRACKET op
=expression CLOSE_BRACKET)
       
((OPERATOR_MULT | OPERATOR_DIV) factor)?;


get: GET OPEN_BRACKET expression COMMA TEXT CLOSE_BRACKET;

Geoff Groos

unread,
Mar 7, 2019, 2:47:09 PM3/7/19
to antlr-discussion
I'm having trouble understanding your question.

I think I must use JSON instead of MAP

so you would like to write and parse the program

PROGRAM aa{
PRINT GET
(
'{"aaaa":"17"}' , "aaaa")  ;
}

Is that it?

You're current parsing system will parse this, so if you're encountering exceptions it would be from the implementation of the visit-listeners. Have you tried putting a breakpoint there to see if you can figure out whats going on?

I suggest you follow the standard SSCCE approach to this problem, with something like

"using this grammar, to parse this program, I expect to get X but infact I get Y"

Then I will understand your problem much better.

Cheers!
Reply all
Reply to author
Forward
0 new messages