Adding grammar file and pom.xml
This commit is contained in:
parent
cdd7972f27
commit
d5f1b9a23a
59
Demo.g4
Normal file
59
Demo.g4
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
grammar Demo;
|
||||||
|
|
||||||
|
program: programPart+;
|
||||||
|
|
||||||
|
programPart: statement #MainStatement
|
||||||
|
| functionDefinition #ProgPartFunctionDefinition
|
||||||
|
;
|
||||||
|
|
||||||
|
statement : println ';'
|
||||||
|
| print ';'
|
||||||
|
| varDeclaration ';'
|
||||||
|
| assignment ';'
|
||||||
|
| branch
|
||||||
|
;
|
||||||
|
|
||||||
|
branch : 'if' '(' condition=expression ')' onTrue=block 'else' onFalse=block ;
|
||||||
|
|
||||||
|
block : '{' statementList '}' ;
|
||||||
|
|
||||||
|
expression: left=expression '/' right=expression #Div
|
||||||
|
| left=expression '*' right=expression #Mult
|
||||||
|
| left=expression '-' right=expression #Minus
|
||||||
|
| left=expression '+' right=expression #Plus
|
||||||
|
| left=expression operator=('<' | '<=' | '>' | '>=') right=expression #Relational
|
||||||
|
| left=expression operator='&&' right=expression #And
|
||||||
|
| left=expression operator='||' right=expression #Or
|
||||||
|
| number=NUMBER #Number
|
||||||
|
| txt=STRING #String
|
||||||
|
| varName=IDENTIFIER #Variable
|
||||||
|
| functionCall #FuncCallExpression
|
||||||
|
;
|
||||||
|
|
||||||
|
varDeclaration: 'int' varName=IDENTIFIER ;
|
||||||
|
|
||||||
|
assignment: varName=IDENTIFIER '=' expr=expression ;
|
||||||
|
|
||||||
|
println: 'println(' argument=expression ')' ;
|
||||||
|
|
||||||
|
print: 'print(' argument=expression ')' ;
|
||||||
|
|
||||||
|
functionDefinition: 'int' funcName=IDENTIFIER '(' params=parameters')' '{' statements=statementList 'return' retValue=expression ';' '}' ;
|
||||||
|
|
||||||
|
statementList : (statement)* ;
|
||||||
|
|
||||||
|
parameters:
|
||||||
|
| declarations+=varDeclaration (',' declarations+=varDeclaration)*
|
||||||
|
;
|
||||||
|
|
||||||
|
functionCall: funcName=IDENTIFIER '(' args=expressionList ')';
|
||||||
|
|
||||||
|
expressionList:
|
||||||
|
| expressions+=expression (',' expressions+=expression)*
|
||||||
|
;
|
||||||
|
|
||||||
|
// Terminals
|
||||||
|
IDENTIFIER: [A-Za-z][A-Za-z0-9]*;
|
||||||
|
NUMBER: [0-9]+;
|
||||||
|
WHITESPACE: [ \t\n\r]+ -> skip;
|
||||||
|
STRING: '"' .*? '"';
|
30
pom.xml
Normal file
30
pom.xml
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<groupId>org.example</groupId>
|
||||||
|
<artifactId>antlr-compiler</artifactId>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<modules>
|
||||||
|
<module>parser</module>
|
||||||
|
<module>compiler</module>
|
||||||
|
</modules>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.antlr/antlr4 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.antlr</groupId>
|
||||||
|
<artifactId>antlr4</artifactId>
|
||||||
|
<version>4.7.1</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
Loading…
Reference in New Issue
Block a user