Skip to content

Commit 66e3fda

Browse files
Add 9 non-overlapping grammars and compilation test under antlr_grammars
Signed-off-by: sumittlearnbay <[email protected]>
1 parent 8423928 commit 66e3fda

File tree

9 files changed

+84
-0
lines changed

9 files changed

+84
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
grammar Arithmetic;
2+
expr: expr op=('*'|'/') expr # MulDiv
3+
| expr op=('+'|'-') expr # AddSub
4+
| INT # Int
5+
| '(' expr ')' # Parens ;
6+
INT: [0-9]+;
7+
WS: [ \t\r\n]+ -> skip;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
grammar BooleanExpr;
2+
expr: expr AND expr
3+
| expr OR expr
4+
| NOT expr
5+
| '(' expr ')'
6+
| BOOL ;
7+
AND: 'AND';
8+
OR: 'OR';
9+
NOT: 'NOT';
10+
BOOL: 'TRUE' | 'FALSE';
11+
WS: [ \t\r\n]+ -> skip;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
grammar CSVFlexible;
2+
file: row+ ;
3+
row: value (',' value)* NEWLINE ;
4+
value: QUOTED | TEXT? ;
5+
TEXT: ~[,"\r\n]+ ;
6+
QUOTED: '"' (~["\r\n] | '""')* '"' ;
7+
NEWLINE: '\r'? '\n' ;
8+
WS: [ \t]+ -> skip ;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import org.antlr.v4.runtime.*;
2+
import org.antlr.v4.runtime.tree.*;
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
6+
public class GrammarCompilationTest {
7+
8+
@Test
9+
public void testArithmeticGrammarLoads() throws Exception {
10+
ArithmeticLexer lexer = new ArithmeticLexer(CharStreams.fromString("3+4*5"));
11+
ArithmeticParser parser = new ArithmeticParser(new CommonTokenStream(lexer));
12+
ParseTree tree = parser.expr();
13+
assertNotNull(tree, "Parse tree should not be null");
14+
}
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
grammar JSONMini;
2+
json: value ;
3+
value: STRING | NUMBER | obj | array | 'true' | 'false' | 'null' ;
4+
obj: '{' pair (',' pair)* '}' ;
5+
pair: STRING ':' value ;
6+
array: '[' value (',' value)* ']' ;
7+
STRING: '"' (~["\\] | '\\' .)* '"' ;
8+
NUMBER: '-'? INT ('.' [0-9]+)? ;
9+
fragment INT: '0' | [1-9][0-9]* ;
10+
WS: [ \t\r\n]+ -> skip ;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
grammar MiniConfig;
2+
config: (section | pair)* EOF;
3+
section: '[' NAME ']' ;
4+
pair: NAME '=' VALUE ;
5+
NAME: [a-zA-Z_][a-zA-Z0-9_]* ;
6+
VALUE: ~[\r\n#;]+ ;
7+
WS: [ \t\r\n]+ -> skip ;
8+
COMMENT: ('#'|';').*? -> skip ;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
grammar MiniMarkdown;
2+
doc: (heading | bold | text)+ ;
3+
heading: '#' WS? TEXT NL ;
4+
bold: '**' TEXT '**' ;
5+
text: TEXT NL? ;
6+
TEXT: ~[\r\n#*]+ ;
7+
WS: [ \t]+ -> skip ;
8+
NL: '\r'? '\n' ;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
grammar MiniQuery;
2+
query: 'SELECT' columns 'FROM' table ('WHERE' condition)? EOF ;
3+
columns: '*' | column (',' column)* ;
4+
column: ID ;
5+
table: ID ;
6+
condition: column op value ;
7+
op: '=' | '<' | '>' ;
8+
value: STRING | NUMBER ;
9+
ID: [a-zA-Z_][a-zA-Z0-9_]* ;
10+
STRING: '\'' (~['\r\n])* '\'' ;
11+
NUMBER: [0-9]+ ;
12+
WS: [ \t\r\n]+ -> skip ;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
grammar UnitExpr;
2+
expr: NUMBER UNIT ;
3+
NUMBER: [0-9]+ ('.' [0-9]+)? ;
4+
UNIT: [a-zA-Z/_]+ ;
5+
WS: [ \t\r\n]+ -> skip ;

0 commit comments

Comments
 (0)