1
0

Adding grammar file and pom.xml

This commit is contained in:
Thibaud Gasser 2018-04-05 20:02:25 +02:00
parent cdd7972f27
commit d5f1b9a23a
2 changed files with 89 additions and 0 deletions

59
Demo.g4 Normal file
View 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
View 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>