diff options
Diffstat (limited to 'source/mod_student')
-rw-r--r-- | source/mod_student/lexer.mll | 83 | ||||
-rw-r--r-- | source/mod_student/parser.mly | 22 |
2 files changed, 51 insertions, 54 deletions
diff --git a/source/mod_student/lexer.mll b/source/mod_student/lexer.mll index a62b8ad..806cfb8 100644 --- a/source/mod_student/lexer.mll +++ b/source/mod_student/lexer.mll @@ -31,6 +31,7 @@ *) let identifier = ['a'-'z' '_' 'A' - 'Z']['_' 'A' - 'Z' 'a'-'z' '0'-'9']* + let integer = ['0'-'9']* (* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ----------------------------------------------------------------------------- *) @@ -54,60 +55,40 @@ (* vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv * Miejsce na twoje reguĊy *) - | "(" - { LPAREN } - | ")" - { RPAREN } - | "{" - { LBRACKET } - | "{" - { RBRACKET } - | ":" - { COLON } - | "+" - { OP_PLUS } - | "-" - { OP_MINUS } - | "*" - { OP_MULT } - | "/" - { OP_DIV } - | "%" - { OP_REM } - | "&" - { OP_AND } - | "|" - { OP_OR } - | "==" - { OP_EQ } - | "!=" - { OP_NEQ } - | "<=" - { OP_LE } - | ">=" - { OP_GE } - | "<" - { OP_LT } - | ">" - { OP_GT } - | "=" - { ASSIGN } - | "if" - { IF } - | "else" - { ELSE } - | "while" - { WHILE } - | "return" - { RETURN } - | "length" - { LENGTH } - | identifier as id - { IDENTIFIER id } + | "(" { LPAREN } + | ")" { RPAREN } + | "{" { LBRACKET } + | "{" { RBRACKET } + | "[" { LSBRACKET } + | "]" { RSBRACKET } + | ":" { COLON } + | "+" { OP_PLUS } + | "-" { OP_MINUS } + | "*" { OP_MULT } + | "/" { OP_DIV } + | "%" { OP_REM } + | "&" { OP_AND } + | "|" { OP_OR } + | "==" { OP_EQ } + | "!=" { OP_NEQ } + | "<=" { OP_LE } + | ">=" { OP_GE } + | "<" { OP_LT } + | ">" { OP_GT } + | "=" { ASSIGN } + | "if" { IF } + | "else" { ELSE } + | "while" { WHILE } + | "return" { RETURN } + | "length" { LENGTH } + | "int" { T_INT } + | "bool" { T_BOOL } + | identifier as id { IDENTIFIER id } + | integer as i { INT (int_of_string i) } (* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ----------------------------------------------------------------------------- *) - + | " " {token lexbuf} | _ { handleError (Lexing.lexeme_start_p lexbuf) (Lexing.lexeme lexbuf) } diff --git a/source/mod_student/parser.mly b/source/mod_student/parser.mly index 4889d11..0fb4ee0 100644 --- a/source/mod_student/parser.mly +++ b/source/mod_student/parser.mly @@ -34,6 +34,8 @@ let mkTag = %token RPAREN %token LBRACKET %token RBRACKET +%token LSBRACKET +%token RSBRACKET %token COLON %token OP_PLUS %token OP_MINUS @@ -58,6 +60,8 @@ let mkTag = %token <string>IDENTIFIER %token <char>CHAR %token <bool>BOOL +%token T_BOOL +%token T_INT (* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ----------------------------------------------------------------------------- *) @@ -77,11 +81,23 @@ file: { ModuleDefinition {global_declarations=[] } } func: - | id=identifier LPAREN RPAREN + | id=identifier LPAREN parameters=list(argument) RPAREN { GDECL_Function { loc=Location( {line=0;column=0;file=""}); - id=id;formal_parameters=[]; return_types=[];body=None} } + id=id;formal_parameters=parameters; return_types=[];body=None} } +argument: + | id = identifier COLON t=typ + { VarDecl {loc=mkLocation $startpos;id=id;tp=t} } +typ: + | t=base_type { t } + | t=typ LSBRACKET option(INT) RSBRACKET + { TEXPR_Array { loc=mkLocation $startpos; + sub=t;dim=None } } +base_type: + | T_INT {TEXPR_Int {loc=mkLocation $startpos}} + | T_BOOL {TEXPR_Bool {loc=mkLocation $startpos}} + identifier: - | IDENTIFIER + | IDENTIFIER { Identifier $1 } (* |