Note that there are some explanatory texts on larger screens.

plurals
  1. POAntlr4 Grammar/Rules - issue with solving BASIC print variable
    primarykey
    data
    text
    <p>The scenario is that I want to create a BASIC (high level) language using ANTRL4.</p> <p>The test input below is the creation of a variable called C$ and assigning an integer value. The value assignment works. The print statement works except where concatenating the variable to it:-</p> <pre><code> ************ TEST CASE **************** </code></pre> <p>$C=15;</p> <p>print "dangerdanger!"; # print works</p> <p>print "Number of GB left=" + $C;</p> <p><img src="https://i.stack.imgur.com/yHMQ0.png" alt="Parse Tree Inspector"></p> <p>Using a Parse Tree Inspector I can see assignments are working fine but when it gets to the identification of the variable in the string it seems there is a mismatched input '+' expecting STMTEND.</p> <p>I wondered if anyone could help me out here and see what adjustment I need to make to my rules and grammar to solve this issue.</p> <p>Many thanks in advance.</p> <p>Kevin PS. As a side issue I would rather have C$ than $C but early days...</p> <pre><code>********RULES************ VARNAME : '$'('A'..'Z')* ; CONCAT : '+' ; STMTEND : SEMICOLON NEWLINE* | NEWLINE+ ; STRING : SQUOTED_STRING (CONCAT SQUOTED_STRING | CONCAT VARNAME)* | DQUOTED_STRING (CONCAT DQUOTED_STRING | CONCAT VARNAME)* ; fragment SQUOTED_STRING : '\'' (~['])* '\'' ; fragment DQUOTED_STRING : '"' ( ESC_SEQ| ~('\\'|'"') )* '"' ; fragment ESC_SEQ : '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\') | UNICODE_ESC | OCTAL_ESC ; fragment OCTAL_ESC : '\\' ('0'..'3') ('0'..'7') ('0'..'7') | '\\' ('0'..'7') ('0'..'7') | '\\' ('0'..'7') ; fragment HEX_DIGIT : '0x' ('0'..'9' | 'a'..'f' | 'A'..'F')+ ; fragment UNICODE_ESC : '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT ; SEMICOLON : ';' ; NEWLINE : '\r'?'\n' ************GRAMMAR************ print_command : PRINT STRING STMTEND #printCommandLabel ; assignment : VARNAME EQUALS INTEGER STMTEND #assignInteger | VARNAME EQUALS STRING STMTEND #assignString ; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload