Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
grammar Arithmetic;
expr: expr op=('*'|'/') expr # MulDiv
| expr op=('+'|'-') expr # AddSub
| INT # Int
| '(' expr ')' # Parens ;
INT: [0-9]+;
WS: [ \t\r\n]+ -> skip;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
grammar BooleanExpr;
expr: expr AND expr
| expr OR expr
| NOT expr
| '(' expr ')'
| BOOL ;
AND: 'AND';
OR: 'OR';
NOT: 'NOT';
BOOL: 'TRUE' | 'FALSE';
WS: [ \t\r\n]+ -> skip;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
grammar CSVFlexible;
file: row+ ;
row: value (',' value)* NEWLINE ;
value: QUOTED | TEXT? ;
TEXT: ~[,"\r\n]+ ;
QUOTED: '"' (~["\r\n] | '""')* '"' ;
NEWLINE: '\r'? '\n' ;
WS: [ \t]+ -> skip ;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
grammar ChatCommand;

// Entry rule
command
: SLASH CMD (WS ARG)* EOF
;

// Lexer rules
SLASH : '/' ;
CMD : [a-zA-Z]+ ;
ARG : [a-zA-Z0-9@._-]+ ;
WS : [ \t\r\n]+ -> skip ;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class GrammarCompilationTest {

@Test
public void testArithmeticGrammarLoads() throws Exception {
ArithmeticLexer lexer = new ArithmeticLexer(CharStreams.fromString("3+4*5"));
ArithmeticParser parser = new ArithmeticParser(new CommonTokenStream(lexer));
ParseTree tree = parser.expr();
assertNotNull(tree, "Parse tree should not be null");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
grammar JSONMini;
json: value ;
value: STRING | NUMBER | obj | array | 'true' | 'false' | 'null' ;
obj: '{' pair (',' pair)* '}' ;
pair: STRING ':' value ;
array: '[' value (',' value)* ']' ;
STRING: '"' (~["\\] | '\\' .)* '"' ;
NUMBER: '-'? INT ('.' [0-9]+)? ;
fragment INT: '0' | [1-9][0-9]* ;
WS: [ \t\r\n]+ -> skip ;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
grammar MiniConfig;
config: (section | pair)* EOF;
section: '[' NAME ']' ;
pair: NAME '=' VALUE ;
NAME: [a-zA-Z_][a-zA-Z0-9_]* ;
VALUE: ~[\r\n#;]+ ;
WS: [ \t\r\n]+ -> skip ;
COMMENT: ('#'|';').*? -> skip ;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
grammar MiniMarkdown;
doc: (heading | bold | text)+ ;
heading: '#' WS? TEXT NL ;
bold: '**' TEXT '**' ;
text: TEXT NL? ;
TEXT: ~[\r\n#*]+ ;
WS: [ \t]+ -> skip ;
NL: '\r'? '\n' ;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
grammar MiniQuery;
query: 'SELECT' columns 'FROM' table ('WHERE' condition)? EOF ;
columns: '*' | column (',' column)* ;
column: ID ;
table: ID ;
condition: column op value ;
op: '=' | '<' | '>' ;
value: STRING | NUMBER ;
ID: [a-zA-Z_][a-zA-Z0-9_]* ;
STRING: '\'' (~['\r\n])* '\'' ;
NUMBER: [0-9]+ ;
WS: [ \t\r\n]+ -> skip ;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
grammar UnitExpr;
expr: NUMBER UNIT ;
NUMBER: [0-9]+ ('.' [0-9]+)? ;
UNIT: [a-zA-Z/_]+ ;
WS: [ \t\r\n]+ -> skip ;
Loading