Commits

Anonymous committed d27f888

this grammar is radically incorrect, but it is mostly a transcription of what was in the LLR grammar from the web

Comments (0)

Files changed (4)

   | Eint of int
   | ENull 
   | EString of string
+  | EPair of (expr * expr)
+  | ENone
+
+type oper = 
+  | OEq
+  | OEEq
+  | OMul
+  | OPlus
+  | OInc
+  | ORShift
+  | OLShift
+  | ONeq
+  | OLT 
+  | OGT
+
+type stmt = 
+  | SIf
+  | SWhile
+  | SFor
+  | SIfElse
+  | SBreak
+  | SContinue
+  | SWith
+  | SReturn
+  | SEndSemi
+
+type elem = 
+  | EFunc
+  | EStmt
+
+type prog = elem list
 
 (*type expr =
     EInt of int
 }
 rule token = parse
   [' ' '\t' '\n' '\r']	{ token lexbuf }
-| "class" { CLASS }
 | "if" { IF }
-| "then" { THEN }
 | "else" { ELSE }
-| "end" { END }
-| "def" { DEF }
-| "nil" { NIL }
-| "self" { SELF }
-| "begin" { BEGIN }
+| "bot" { BOT }
+| "while" { WHILE }
+| "for" { FOR }
+| "function" { FUNC }
 | "new" { NEW }
-| "bot" { BOT }
-| '<' { INHERITS }
+| "delete" { DELETE }
+| "false" { FALSE }
+| "true" { TRUE}
+| "null" { NULL }
+| "var" { VAR }
+| "this" { THIS }
+| '.' { DOT }
 | '(' { LP }
 | ')' { RP }
+| '{' { LB }
+| '}' { RB }
 | ',' { COMMA }
 | ':' { COLON }
 | ';' { SEMI }
 | '.' { DOT }
 | '=' { EQ }
+| '?' { QT }
+| '|' { BPIPE }
+| '^' { BXOR }
+| '&' { BAND }
+| '+' { PLUS }
+| '-' { MINUS }
+| '*' { MUL }
+| '[' { LBAR }
+| ']' { RBAR }
+| '>' { GT }
+| '<' { LT }
+| ">>" { RSHIFT }
+| "<<" { LSHIFT }
+| "!==" { NEEQ }
+| "!=" { NEQ }
+| "==" { EEQ }
+| "===" {EEEQ }
+| "&&" { LBAND }
+| "||" { LBPIPE }
+| "++" { DPLUS }
 | '"' [^'"']* '"' as lxm { STR(String.sub lxm 1 ((String.length lxm) - 2)) }
 | ['0'-'9']+ as lxm { INT(int_of_string lxm) }
 | ['A'-'Z''a'-'z''+''-''*''/''_''!''?']+ as lxm { ID(lxm) }
 let parse_file name =
   let chan = open_in name in
   let lexbuf = Lexing.from_channel chan in
-  let p = Parser.main Lexer.token lexbuf in
+  let p = Parser.program Lexer.token lexbuf in
     close_in chan;
     p
 
 %}
 
 %token <int> INT
-%token <string> STR ID FID
-%token CLASS IF THEN ELSE DEF END NEW NIL BOT
-%token LT LP RP COMMA SEMI DOT EQ EOF SELF BEGIN INHERITS COLON
-%type <Ast.expr> main
-%start main
+%token <string> STR ID
+%token IF THEN ELSE DEF END NEW NIL BOT FUNC LB RB MUL NEW DELETE PLUS MINUS DOT DPLUS
+%token LT LP RP COMMA SEMI DOT EQ EOF SELF BEGIN COLON LBAR RBAR FALSE TRUE NULL THIS
+%token RSHIFT LSHIFT NEQ EEQ EEEQ BAND BXOR BPIPE LBAND LBPIPE QT FOR VAR WHILE GT LT NEEQ
+%type <Ast.prog> program
+%type <Ast.elem> element
+%type <string list> parameterlistopt
+%type <string list> parameterlist
+%type <Ast.stmt list> compoundstatement
+%type <Ast.stmt list> statements
+%type <Ast.stmt> statement
+%type <Ast.expr> condition
+%type <Ast.expr> expression
+%type <Ast.expr> assignmentexpression
+%type <Ast.oper> equalityualityoperator
+%start program
 %%
-main:
- { ENull }
-;
+
+program:
+ { [] }
+ | element program { $1::$2 }
+;
+
+element:
+ 	FUNC ID LP parameterlistopt RP compoundstatement { EFunc }
+ 	| statement { EStmt }
+;
+
+parameterlist:
+	ID { [$1] }
+	| ID COMMA parameterlist { $1::$3 }
+;
+
+parameterlistopt:
+ { [] }
+ | parameterlist { $1 }
+;
+
+compoundstatement:
+ LB statements RB { $2 }
+;
+
+statements:
+ { [] }
+ | statement statements { $1::$2 }
+;
+
+multiplicativeoperator:
+	MUL { OMul }
+;
+
+multiplicativeexpression:
+	unaryexpression { $1 } 
+	| unaryexpression multiplicativeoperator multiplicativeexpression { ENone }
+;
+
+additiveexpression:
+	multiplicativeexpression { $1 }
+	| multiplicativeexpression PLUS additiveexpression { ENone }
+	| multiplicativeexpression MINUS additiveexpression { ENone }
+;
+
+primaryexpression:
+	LP expression RP { ENone }
+	| ID { ENone }
+	| INT { ENone }
+	| STR { ENone }
+	| FALSE { ENone }
+	| TRUE { ENone }
+	| NULL { ENone }
+	| THIS { ENone }
+;
+
+memberexpression:
+	primaryexpression { $1 }
+	| primaryexpression DOT memberexpression { ENone }
+	| primaryexpression LBAR expression RBAR { ENone }
+	| primaryexpression LP argumentlistopt RP { ENone }
+;
+
+unaryoperator:
+	PLUS { OPlus }
+;
+
+incrementoperator:
+	DPLUS { OInc }
+;
+
+constructorcall:
+	ID { ENone }
+	| ID LP argumentlistopt RP { ENone }
+	| ID DOT constructorcall { ENone }
+;
+
+constructor:
+	THIS DOT constructorcall { ENone }
+	| constructorcall { ENone }
+;
+
+shiftoperator:
+	RSHIFT { ORShift }
+	| LSHIFT { OLShift }
+;
+
+unaryexpression:
+	memberexpression { $1 }
+	| unaryoperator unaryexpression { ENone }
+	| MINUS unaryexpression { ENone }
+	| incrementoperator memberexpression { ENone }
+	| memberexpression incrementoperator { ENone }
+	| NEW constructor { ENone }
+	| DELETE memberexpression { ENone }
+;
+
+shiftexpression:
+	additiveexpression { $1 }
+	| additiveexpression shiftoperator shiftexpression { ENone }
+;
+
+relationaloperator:
+	NEQ { ONeq }
+;
+
+relationalexpression:
+	shiftexpression { $1 }
+	| relationalexpression relationaloperator shiftexpression { ENone }
+;
+
+equalityualityoperator:
+	EEQ { OEq }
+	| EEEQ { OEEq }
+;
+
+equalityexpression:
+	relationalexpression { $1 }
+	| relationalexpression equalityualityoperator equalityexpression { ENone }
+;
+
+bitwiseandexpression:
+	equalityexpression { $1 }
+	| equalityexpression BAND bitwiseandexpression { ENone }
+;
+
+bitwisexorexpression:
+	bitwiseandexpression { $1 }
+	| bitwiseandexpression BXOR bitwisexorexpression { ENone }
+;
+
+bitwiseorexpression:
+	bitwisexorexpression { $1 }
+	| bitwisexorexpression BPIPE bitwiseorexpression { ENone }
+;
+
+andexpression:
+	bitwiseorexpression { $1 }
+	| bitwiseorexpression LBAND andexpression { ENone }
+;
+
+argumentlistopt:
+	{ [] }
+	| argumentlist { $1 }
+;
+
+argumentlist:
+	assignmentexpression { [$1] }
+	| assignmentexpression COMMA argumentlist { $1::$3 }
+;
+
+orexpression:
+	andexpression { $1 }
+	| andexpression LBPIPE orexpression { ENone }
+;
+
+conditionalexpression:
+	orexpression { $1 }
+	| orexpression QT assignmentexpression COLON assignmentexpression { ENone }
+;
+
+assignmentexpression:
+	conditionalexpression { $1 }
+	| conditionalexpression EQ assignmentexpression { ENone }
+;
+
+expression:
+	assignmentexpression { $1 }
+	| assignmentexpression COMMA expression { EPair($1, $3) }
+;
+
+expressionopt:
+	{ ENone }
+	| expression { $1 }
+;
+
+condition:
+ LP expression RP { $2 }
+;
+
+forparen:
+	FOR LP { () }
+;
+
+variable:
+	ID { ENone }
+	| ID EQ assignmentexpression { ENone }
+;
+
+variables:
+	variable { [$1] }
+	| variable COMMA variables { $1::$3 }
+;
+
+variablesorexpression:
+	VAR variables { ENone }
+	| expression { $1 }
+;
+
+forbegin:
+	forparen variablesorexpression { $2 }
+;
+
+statement:
+    SEMI { SEndSemi }
+  | IF condition statement { SIf }
+  | IF condition statement ELSE statement { SIf }
+  | WHILE condition statement { SWhile }
+;