Skip to content

Commit

Permalink
Updated full parser example
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-staal committed Mar 7, 2024
1 parent 94b30f4 commit 29fa089
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ declarator

direct_declarator
: IDENTIFIER {
$$ = new Identifier(*$1);
$$ = new Identifier(std::move(*$1));
delete $1;
}
| direct_declarator '(' ')' {
Expand Down
47 changes: 27 additions & 20 deletions src/parser_full.y.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@
%code requires{
#include "ast.hpp"

using namespace ast;

extern Node* g_root;
extern FILE* yyin;
int yylex(void);
void yyerror(const char *);
void yyerror(const char*);
int yylex_destroy(void);
}

// Represents the value associated with any kind of AST node.
%union{
Node* node;
NodeList* nodes;
int number_int;
double number_float;
std::string* string;
yytokentype token;
Node* node;
NodeList* node_list;
int number_int;
double number_float;
std::string* string;
TypeSpecifier type_specifier;
yytokentype token;
}

%token IDENTIFIER INT_CONSTANT FLOAT_CONSTANT STRING_LITERAL
Expand All @@ -36,13 +40,16 @@
%type <node> identifier_list type_name abstract_declarator direct_abstract_declarator initializer initializer_list statement labeled_statement
%type <node> compound_statement declaration_list expression_statement selection_statement iteration_statement jump_statement

%type <nodes> statement_list
%type <node_list> statement_list

%type <string> unary_operator assignment_operator storage_class_specifier

%type <number_int> INT_CONSTANT STRING_LITERAL
%type <number_float> FLOAT_CONSTANT
%type <string> IDENTIFIER
%type <type_specifier> type_specifier
// TODO: Make a better type for this (only needed for advanced features)
%type <type_specifier> declaration_specifiers


%start ROOT
Expand All @@ -64,7 +71,7 @@ external_declaration
function_definition
: declaration_specifiers declarator declaration_list compound_statement
| declaration_specifiers declarator compound_statement {
$$ = new FunctionDefinition($1, $2, $3);
$$ = new FunctionDefinition($1, NodePtr($2), NodePtr($3));
}
| declarator declaration_list compound_statement
| declarator compound_statement
Expand Down Expand Up @@ -245,9 +252,7 @@ type_specifier
: VOID
| CHAR
| SHORT
| INT {
$$ = new TypeSpecifier("int");
}
| INT { $$ = TypeSpecifier::INT; }
| LONG
| FLOAT
| DOUBLE
Expand Down Expand Up @@ -312,7 +317,7 @@ declarator

direct_declarator
: IDENTIFIER {
$$ = new Identifier(*$1);
$$ = new Identifier(std::move(*$1));
delete $1;
}
| '(' declarator ')'
Expand All @@ -321,7 +326,7 @@ direct_declarator
| direct_declarator '(' parameter_list ')'
| direct_declarator '(' identifier_list ')'
| direct_declarator '(' ')' {
$$ = new DirectDeclarator($1);
$$ = new DirectDeclarator(NodePtr($1));
}
;

Expand Down Expand Up @@ -419,8 +424,8 @@ declaration_list
;

statement_list
: statement { $$ = new NodeList($1); }
| statement_list statement { $1->PushBack($2); $$=$1; }
: statement { $$ = new NodeList(NodePtr($1)); }
| statement_list statement { $1->PushBack(NodePtr($2)); $$=$1; }
;

expression_statement
Expand Down Expand Up @@ -449,17 +454,17 @@ jump_statement
$$ = new ReturnStatement(nullptr);
}
| RETURN expression ';' {
$$ = new ReturnStatement($2);
$$ = new ReturnStatement(NodePtr($2));
}
;



%%

Node *g_root;
Node* g_root;

Node *ParseAST(std::string file_name)
NodePtr ParseAST(std::string file_name)
{
yyin = fopen(file_name.c_str(), "r");
if(yyin == NULL){
Expand All @@ -468,5 +473,7 @@ Node *ParseAST(std::string file_name)
}
g_root = nullptr;
yyparse();
return g_root;
fclose(yyin);
yylex_destroy();
return NodePtr(g_root);
}

0 comments on commit 29fa089

Please sign in to comment.