Skip to content

Commit

Permalink
complete parser?
Browse files Browse the repository at this point in the history
  • Loading branch information
SantriptaSharma committed Oct 21, 2023
1 parent cecb771 commit 4a33b18
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 8 deletions.
16 changes: 12 additions & 4 deletions 15_A3.l
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
#endif
%}

KEYWORD char|else|for|if|int|return|void
IDENT [a-zA-Z\_][0-9a-zA-Z\_]*
PUNCT \[|\]|\(|\)|\{|\}|->|&|\*|\+|-|\/|%|!|\?|\<|\>|<=|>=|==|!=|&&|\\|\||=|:|;|,
ESCAPE \\'|\\\?|\\\"|\\\\|\\a|\\b|\\f|\\n|\\r|\\t|\\v
CHAR [^\\'\n]|{ESCAPE}
CONST ([\+-]?[1-9][0-9]*)|[0-9]+|'{CHAR}+'
CHARCONST '{CHAR}+'
INTCONST ([\+-]?[1-9][0-9]*)|[0-9]+
STRCHAR [^\"\\\n]|{ESCAPE}
STRLIT \"{STRCHAR}*\"
COMMENTSINGLE \/\/([^\n])*\n
Expand All @@ -18,8 +17,17 @@ COMMENTMULTI \/\*([^\*]|\*[^\/])*\*\/


%%
"void" { return VOID; }
"char" { return CHAR; }
"int" { return INT; }
"if" { return IF; }
"else" { return ELSE; }
"for" { return FOR; }
"return" { return RETURN; }

{IDENT} { return IDENTIFIER;}
{CONST} { return CONSTANT; }
{INTCONST} { return INTCONST; }
{CHARCONST} { return CHARCONST; }
{STRLIT} { return STRING_LITERAL;}

"->" { return ARROW; }
Expand Down
107 changes: 103 additions & 4 deletions 15_A3.y
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,39 @@
%}

/* %token KEYWORD */

%token IDENTIFIER
%token CONSTANT
%token INTCONST
%token CHARCONST
%token STRING_LITERAL
/* %token PUNCTUATOR */

/* %token PUNCTUATOR */
%token ARROW
%token GEQ
%token LEQ
%token CEQ
%token NEQ
%token LAND
%token LOR
%token VOID
%token CHAR
%token INT
%token IF
%token ELSE
%token FOR
%token RETURN

%start expression
%start translation_unit

%%
/* expressions */
constant:
INTCONST
| CHARCONST

primary_expression:
IDENTIFIER
| CONSTANT
| constant
| STRING_LITERAL
| '(' expression ')'

Expand Down Expand Up @@ -89,6 +103,91 @@ assignment_expression:

expression:
assignment_expression

/* Declarations */

declaration:
type_specifier init_declarator

init_declarator:
declarator
| declarator '=' initalizer

type_specifier:
VOID
| CHAR
| INT

declarator:
pointer direct_declarator
| direct_declarator

direct_declarator:
IDENTIFIER
| IDENTIFIER '[' INTCONST ']'
| IDENTIFIER '(' parameter_list ')'
| IDENTIFIER '(' ')'

pointer:
'*'

parameter_list:
parameter_declaration
| parameter_list ',' parameter_declaration

parameter_declaration:
type_specifier pointer IDENTIFIER
| type_specifier IDENTIFIER
| type_specifier pointer
| type_specifier

initalizer:
assignment_expression

/* Statements */
statement:
compound_statement
| expression_statement
| selection_statement
| iteration_statement
| jump_statement

compound_statement:
'{' '}'
| '{' block_item_list '}'

block_item_list:
block_item
| block_item_list block_item

block_item:
declaration
| statement

opt_expression:
expression
| /* empty */

expression_statement:
opt_expression ';'

selection_statement:
IF '(' expression ')' statement
| IF '(' expression ')' statement ELSE statement

iteration_statement:
FOR '(' opt_expression ';' opt_expression ';' opt_expression ')' statement

jump_statement:
RETURN opt_expression ';'

/* TLU */
translation_unit:
function_definition
| declaration

function_definition:
type_specifier declarator compound_statement
%%

void yyerror(char *s) {
Expand Down
17 changes: 17 additions & 0 deletions add.nc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Add two numbers
int main() {
int notmain() {
int d = 4 ? "ok" : "not ok";
}

int x = 2;
int y = 3;
int z;
z = x + y;
printInt(x);
printStr("+");
printInt(y);
printStr(" = ");
printInt(z);
return 0;
}

0 comments on commit 4a33b18

Please sign in to comment.