From 86d0833fb238bcb63968e677d645f896da74fbc5 Mon Sep 17 00:00:00 2001 From: Paweł Dybiec Date: Sun, 4 Nov 2018 13:21:36 +0100 Subject: parse function arguments --- source/mod_student/lexer.mll | 83 +++++++++++++++++-------------------------- source/mod_student/parser.mly | 22 ++++++++++-- 2 files changed, 51 insertions(+), 54 deletions(-) (limited to 'source') 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 IDENTIFIER %token CHAR %token 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 } (* -- cgit 1.4.1