Missing variable in Antlr4 parser

Missing variable in Antlr4 parser



I am trying to build a symbol table for a language using Antlr4. I have the following rule in my grammar file.


/* Global String Declaration */
//string_decl : STRING id ASSIGN str SEMICOLON ;
string_decl returns [StrEntry s] : STRING id ASSIGN ex=str SEMICOLON
$s = new StrEntry(); s.addID($id.text); s.addValue($ex.text); ;



I also created a StrEntry class (a dummy implementation)


public class StrEntry


String value;
String id;
String type;


void addID(String x)
id = x;


void addValue(String c)
value = c;





When I compile (javac *.java) I get the following error:


javac *.java


MicroParser.java:382: error: cannot find symbol
((String_declContext)_localctx).s = new StrEntry(); s.addID((((String_declContext)_localctx).id!=null? _input.getText(((String_declContext)_localctx).id.start, ((String_declContext)_localctx).id.stop):null)); s.addValue((((String_declContext)_localctx).ex!=null?_input.getText(((String_declContext)_localctx).ex.start, ((String_declContext)_localctx).ex.stop):null));
^
symbol: variable s
location: class MicroParser



It says variable s of type StrEntry is missing, but I have defined it in my grammar file. I don't think editing it in MicroParser.java file is a good idea as it was generated by Antlr4.



What should I do?






Have you checked that your generated parser contains the code for you StrEntry class. This seems a pure Java problem with the Java compiler not finding a symbol as it is not know at point of first use.

– Mike Lischke
Sep 15 '18 at 10:41




1 Answer
1


$s = new StrEntry(); s.addID($id.text); s.addValue($ex.text)



Here you're using both $s (which will be translated to _localctx.s in the generated Java code) and s (which will remain just s). The latter is the symbol that the compiler can't find because there is no variable with that name defined in that block.


$s


_localctx.s


s


s



In other words, you just need to consistently use $s instead of s and it will work fine.


$s


s



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ