Skip to content

Commit

Permalink
creating identifier type with line numbers.
Browse files Browse the repository at this point in the history
  • Loading branch information
thejoshwolfe committed Sep 18, 2010
1 parent 6187b1e commit a03730f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 30 deletions.
63 changes: 36 additions & 27 deletions parser.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#ifndef _PARSER_H_
#define _PARSER_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>


struct Program;
struct Identifier;
struct ClassList;
struct ClassDeclaration;
struct ClassBlock;
Expand Down Expand Up @@ -44,9 +43,16 @@ struct PrimaryExpression;


struct Program {
char * id;
Identifier * identifier;
ClassList * class_list;
Program(char* id, ClassList * class_list) : id(id), class_list(class_list) {}
Program(Identifier * identifier, ClassList * class_list)
: identifier(identifier), class_list(class_list) {}
};

struct Identifier {
std::string text;
int line_number;
Identifier(std::string text, int line_number) : text(text), line_number(line_number) {}
};

struct ClassList {
Expand All @@ -56,11 +62,11 @@ struct ClassList {
};

struct ClassDeclaration {
char * id;
char * parent_id;
Identifier * identifier;
Identifier * parent_identifier;
ClassBlock * class_block;
ClassDeclaration(char * id, char * parent_id, ClassBlock * class_block)
: id(id), parent_id(parent_id), class_block(class_block) {}
ClassDeclaration(Identifier * identifier, Identifier * parent_identifier, ClassBlock * class_block)
: identifier(identifier), parent_identifier(parent_identifier), class_block(class_block) {}
};

struct ClassBlock {
Expand All @@ -85,21 +91,22 @@ struct VariableDeclaration {
};

struct IdentifierList {
char * item;
Identifier * item;
IdentifierList * next;
IdentifierList(char * item, IdentifierList * next)
IdentifierList(Identifier * item, IdentifierList * next)
: item(item), next(next) {}
};

struct TypeDenoter {
enum Type {INTEGER, REAL, CHAR, BOOLEAN, CLASS, ARRAY};
Type type;
union {
char * class_id;
Identifier * class_identifier;
ArrayType * array_type;
};
TypeDenoter(Type type) : type(type) {}
TypeDenoter(char * class_id) : type(CLASS), class_id(class_id) {}
TypeDenoter(Identifier * class_identifier)
: type(CLASS), class_identifier(class_identifier) {}
TypeDenoter(ArrayType * array_type) : type(ARRAY), array_type(array_type) {}
};

Expand All @@ -118,12 +125,12 @@ struct FunctionDeclarationList {
};

struct FunctionDeclaration {
char * id;
Identifier * identifier;
VariableDeclarationList * parameter_list;
TypeDenoter * type;
FunctionBlock * block;
FunctionDeclaration(char * id, VariableDeclarationList * parameter_list, TypeDenoter * type, FunctionBlock * block)
: id(id), parameter_list(parameter_list), type(type), block(block) {}
FunctionDeclaration(Identifier * identifier, VariableDeclarationList * parameter_list, TypeDenoter * type, FunctionBlock * block)
: identifier(identifier), parameter_list(parameter_list), type(type), block(block) {}
};

struct FunctionBlock {
Expand Down Expand Up @@ -187,11 +194,11 @@ struct VariableAccess {
enum Type {IDENTIFIER, INDEXED_VARIABLE, ATTRIBUTE};
Type type;
union {
char * id;
Identifier * identifier;
IndexedVariable * indexed_variable;
AttributeDesignator * attribute;
};
VariableAccess(char * id) : type(IDENTIFIER), id(id) {}
VariableAccess(Identifier * identifier) : type(IDENTIFIER), identifier(identifier) {}
VariableAccess(IndexedVariable * indexed_variable)
: type(INDEXED_VARIABLE), indexed_variable(indexed_variable) {}
VariableAccess(AttributeDesignator * attribute) : type(ATTRIBUTE), attribute(attribute) {}
Expand Down Expand Up @@ -280,10 +287,10 @@ struct PrimaryExpression {
};

struct FunctionDesignator {
char * id;
Identifier * identifier;
ActualParameterList * parameter_list;
FunctionDesignator(char * id, ActualParameterList * parameter_list)
: id(id), parameter_list(parameter_list) {}
FunctionDesignator(Identifier * identifier, ActualParameterList * parameter_list)
: identifier(identifier), parameter_list(parameter_list) {}
};

struct ActualParameterList {
Expand All @@ -308,8 +315,9 @@ struct ActualParameter {

struct AttributeDesignator {
VariableAccess * owner;
char * id;
AttributeDesignator(VariableAccess * owner, char * id) : owner(owner), id(id) {}
Identifier * identifier;
AttributeDesignator(VariableAccess * owner, Identifier * identifier)
: owner(owner), identifier(identifier) {}
};

struct MethodDesignator {
Expand All @@ -320,11 +328,12 @@ struct MethodDesignator {
};

struct ObjectInstantiation {
char * class_id;
Identifier * class_identifier;
ActualParameterList * parameter_list;
ObjectInstantiation(char * class_id) : class_id(class_id), parameter_list(NULL) {}
ObjectInstantiation(char * class_id, ActualParameterList * parameter_list)
: class_id(class_id), parameter_list(parameter_list) {}
ObjectInstantiation(Identifier * class_identifier)
: class_identifier(class_identifier), parameter_list(NULL) {}
ObjectInstantiation(Identifier * class_identifier, ActualParameterList * parameter_list)
: class_identifier(class_identifier), parameter_list(parameter_list) {}
};


Expand Down
2 changes: 1 addition & 1 deletion pascal.l
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Z [zZ]
"*" return(KEYWORD_STAR);

[a-zA-Z][a-zA-Z0-9]* {
yylval._string = strdup(yytext);
yylval.identifier = new Identifier(yytext, line_number);
return(TOKEN_IDENTIFIER);
}
[0-9]+ {
Expand Down
4 changes: 2 additions & 2 deletions pascal.y
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Program *main_program;
%token KEYWORD_WHILE

%token <_int> TOKEN_DIGIT_SEQUENCE
%token <_string> TOKEN_IDENTIFIER
%token <identifier> TOKEN_IDENTIFIER

%type <type_denoter> type_denoter
%type <identifier_list> identifier_list
Expand Down Expand Up @@ -107,7 +107,7 @@ Program *main_program;

%union {
TypeDenoter * type_denoter;
char * _string;
Identifier * identifier;
IdentifierList * identifier_list;
FunctionDesignator * function_designator;
ActualParameterList * actual_parameter_list;
Expand Down

0 comments on commit a03730f

Please sign in to comment.