set basic repo structure

This commit is contained in:
2024-08-05 07:18:10 +00:00
parent 1711be891f
commit 388e4fda04
27 changed files with 1771 additions and 1 deletions

32
grammar/MXLexer.g4 Normal file
View File

@ -0,0 +1,32 @@
lexer grammar MXLexer;
// Keywords
INT: 'int';
VOID: 'void';
IF: 'if';
ELSE: 'else';
RETURN: 'return';
// Operators
PLUS: '+';
MINUS: '-';
MULTIPLY: '*';
DIVIDE: '/';
ASSIGN: '=';
// Punctuation
LPAREN: '(';
RPAREN: ')';
LBRACE: '{';
RBRACE: '}';
SEMICOLON: ';';
// Identifiers
ID: [a-zA-Z_][a-zA-Z_0-9]*;
// Literals
INT_LITERAL: [0-9]+;
// Whitespace and comments
WS: [ \t\r\n]+ -> skip;
COMMENT: '//' ~[\r\n]* -> skip;

41
grammar/MXParser.g4 Normal file
View File

@ -0,0 +1,41 @@
parser grammar MXParser;
options { tokenVocab=MXLexer; }
mxprog
: function* EOF
;
function
: type ID LPAREN RPAREN block
;
type
: INT
| VOID
;
block
: LBRACE statement* RBRACE
;
statement
: expression SEMICOLON
| returnStmt
| ifStmt
;
expression
: INT_LITERAL
| ID
| expression (PLUS | MINUS | MULTIPLY | DIVIDE) expression
| LPAREN expression RPAREN
;
returnStmt
: RETURN expression? SEMICOLON
;
ifStmt
: IF LPAREN expression RPAREN statement (ELSE statement)?
;

13
grammar/build.sh Executable file
View File

@ -0,0 +1,13 @@
#!/bin/bash
# Get the directory of the script
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
# Change to the script directory
cd "$SCRIPT_DIR"
# Set the output directory
OUTPUT_DIR="../src/semantic/antlr-generated"
# Create the output directory if it doesn't exist
mkdir -p "$OUTPUT_DIR"
# Run ANTLR to generate lexer and parser
antlr4 -Dlanguage=Cpp -no-listener -visitor MXLexer.g4 MXParser.g4 -o "$OUTPUT_DIR"
# Return to the original directory
cd - > /dev/null