Skip to content

Commit

Permalink
Implemented switch-case grammar.
Browse files Browse the repository at this point in the history
  • Loading branch information
mzweilin committed Oct 26, 2013
1 parent 15844fd commit ce058e4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
20 changes: 19 additions & 1 deletion wx4ed/src/pp2/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void yyerror(const char *msg); // standard error-handling routine
%token T_And T_Or T_Null T_Extends T_This T_Interface T_Implements
%token T_While T_For T_If T_Else T_Return T_Break
%token T_New T_NewArray T_Print T_ReadInteger T_ReadLine
%token T_Incr T_Decr
%token T_Incr T_Decr T_Switch T_Case T_Default

%token <identifier> T_Identifier
%token <stringConstant> T_StringConstant
Expand Down Expand Up @@ -126,6 +126,7 @@ void yyerror(const char *msg); // standard error-handling routine
%type <breakStmt> BreakStmt
%type <returnStmt> ReturnStmt
%type <printStmt> PrintStmt
%type <switchStmt> SwitchStmt

%nonassoc '='
%left T_Or
Expand Down Expand Up @@ -260,6 +261,7 @@ Stmt : ExprOpt ';'
| BreakStmt
| ReturnStmt
| PrintStmt
| SwitchStmt
| StmtBlock
;

Expand All @@ -282,6 +284,22 @@ BreakStmt : T_Break ';'
PrintStmt : T_Print '(' ExprPlus ')' ';'
;

SwitchStmt : T_Switch '(' Expr ')' '{' CaseStmts DefaultStmt '}'
;

CaseStmts : CaseStmts CaseStmt
| CaseStmt
;

CaseStmt : T_Case T_IntConstant ':' StmtList
| T_Case T_IntConstant ':'
;

DefaultStmt : T_Default ':' StmtList
| T_Default ':'
| /* empty */
;

ExprOpt : Expr
| // empty
;
Expand Down
6 changes: 4 additions & 2 deletions wx4ed/src/pp2/scanner.l
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ DOUBLE ({INTEGER}"."{DIGIT}*{EXPONENT}?)
BEG_STRING (\"[^"\n]*)
STRING ({BEG_STRING}\")
IDENTIFIER ([a-zA-Z][a-zA-Z_0-9]*)
OPERATOR ([-+/*%=.,;!<>()[\]{}])
OPERATOR ([-+/*%=.,:;!<>()[\]{}])
BEG_COMMENT ("/*")
END_COMMENT ("*/")
SINGLE_COMMENT ("//"[^\n]*)
Expand Down Expand Up @@ -102,7 +102,9 @@ SINGLE_COMMENT ("//"[^\n]*)
"Print" { return T_Print; }
"ReadInteger" { return T_ReadInteger; }
"ReadLine" { return T_ReadLine; }

"switch" { return T_Switch; }
"case" { return T_Case; }
"default" { return T_Default; }


/* -------------------- Operators ----------------------------- */
Expand Down

0 comments on commit ce058e4

Please sign in to comment.