Skip to content

Antlr introduce

benhaben edited this page Jun 9, 2017 · 1 revision

My lisp grammar

grammar Strings;

lispy    : expr*;

expr   : NUMBER | SYMBOL | sexpr | qexpr | string ;

sexpr  : LP expr*  RP;

qexpr  : LB expr* RB ;

NUMBER : '-'?[0-9]+ ;

COMMENT: ';' ~[\r\n]* -> skip ;

string : STRING ;

STRING : '"' ( ~'"' | '\\' '"' )* '"' ;

SYMBOL : ([0-9]|'a'..'z'|'A'.. 'Z'|'_'|ADD|SUB|MUL|DIV|'$'|'!'|'&'|'\\'|'>'|'<'|'>='|'<='|'=='|'!='|'=' )+;

LP  :   '(';
RP  :   ')';
LB  :   '{';
RB  :   '}';
MUL :   '*' ; // assigns token name to '*' used above in grammar
DIV :   '/' ;
ADD :   '+' ;
SUB :   '-' ;

WS : [ \t\r\n]+ -> skip ;

ANTLR Core Notation Syntax Description

  1. x ⇒ Match token, rule reference, or subrule x.

  2. x y …​ z ⇒ Match a sequence of rule elements.

  3. (…​ | …​ | …​) ⇒ Subrule with multiple alternatives.

  4. x? ⇒ Match x or skip it.

  5. x* ⇒ Match x zero or more times.

  6. x+ ⇒ Match x one or more times

  7. r : …​ ; ⇒ Define rule r.

  8. r : …​ | …​ | …​ ; ⇒ Define rule r with multiple alternatives.

*capitals means token

*lower-case means rule

Clone this wiki locally