如何使用Antlr4在C#上编写(Mygrammar)访问者类?

时间:2017-07-24 17:49:30

标签: c# parsing antlr grammar qbasic

我刚刚开始学习解析。我正在尝试使用Visual Studio(QBasic)和Antlr4进行少量c#编译器。我已经完成了我的语法,并生成了像MyGrammarBaseListener.cs MyGrammarLexer.cs ...这样的文件。现在,我应该怎么做VisualStudio?感谢:)

此外,这是我的语法:

grammar gram;

prog : command+;

command : maincommand
        | expressioncommand
        | flowcommand
        ;


//main
maincommand : printcommand
            | inputcommand
            ;
//PRINT "I am:", myname$, myage
//INPUT "What is your name?", yourname$
printcommand : print (IDINT | IDSTR | STRING) NL
             | print (IDINT | IDSTR | STRING) (comma (IDINT | IDSTR | STRING))* NL
             ;
inputcommand : input (IDINT | IDSTR) NL
             | input (STRING comma)? (IDINT | IDSTR) NL
             ;
print : PRINT;
input : INPUT;


//expression
//age = 17
//schoolname = "School"
expressioncommand : intexpression
                  | strexpression
                  ;
intexpression : IDINT equals (IDINT | INT) NL
              | IDINT equals (IDINT | INT) (OPERATORMATH (IDINT | INT))* NL
              ;
strexpression : IDSTR equals (IDSTR | STRING) NL
              ;

equals : EQUALS;


//flow
/* IF a > 3 THEN
PRINT a - 1
INPUT b
ENDIF
*/
flowcommand : ifelseflow
            | whileflow
            ;

ifelseflow : if conditionflow then NL dosomething+ (elseif conditionflow then NL dosomething+)* (else NL dosomething+)? endif NL;
whileflow : while conditionflow NL (dosomething)+ wend NL;
dosomething : command;
conditionflow : (INT | IDINT) OPERATORBOOL (INT | IDINT)
              | (INT | IDINT) equals (INT | IDINT)
              | (STRING | IDSTR) equals (STRING | IDSTR)
              ;
if : IF;
then : THEN;
elseif : ELSEIF;
else : ELSE;
endif : ENDIF;
while : WHILE;
wend : WEND;
comma : COMMA;


//LEXERS
COMMA : ',';
PRINT : 'PRINT';
INPUT : 'INPUT';
EQUALS : '=';
IF : 'IF';
THEN : 'THEN';
ELSEIF : 'ELSEIF';
ELSE : 'ELSE';
ENDIF : 'ENDIF';
WHILE : 'WHILE';
WEND : 'WEND';
INT : [0-9]+;
STRING : '"' .*? '"';
IDINT : [a-zA-Z]+([0-9]*);
IDSTR : [a-zA-Z]+([0-9]*)'$';
NL : '\n';
WS : [ \t\r]+ -> skip;
OPERATORMATH : '+' | '-' | '*' | '/';
OPERATORBOOL : '>' | '<' | '>=' | '<=';

0 个答案:

没有答案