Presentation is loading. Please wait.

Presentation is loading. Please wait.

Semantic Values and Symbol Tables © Allan C. Milne Abertay University v14.6.17.

Similar presentations


Presentation on theme: "Semantic Values and Symbol Tables © Allan C. Milne Abertay University v14.6.17."— Presentation transcript:

1

2 Semantic Values and Symbol Tables © Allan C. Milne Abertay University v14.6.17

3 Agenda. Semantic Values. Handling Identifiers. Symbol Tables.

4 Semantic Values. Microsyntax tokens require both token type and semantic value to be passed back from Lex. –Token type is the yylex() return value. –Semantic value is assigned to the variable yylval. So far all semantic values have been of the same type but most languages will require semantic values of different types; –E.g. integers, reals, strings.

5 The %union Declaration. This is a Yacc declaration that specifies the possible return semantic value types. This implicitly defines YYSTYPE. %union { double number; char* string; }

6 Grammar Component Types. Grammar components that return semantic values must specify their type. –Use a modified %token for tokens. –Use %type for non-terminals. %type expression %token tNUMBER %token tSTRINGVALUE %token tIDENTIFIER

7 Handling In Lex. When a Lex pattern matches a token that has a semantic value then it must –set the semantic value of the required type; and –return the token type as normal. {digit}+("."{digit}+)? { sscanf_s (yytext, "%lf", &yylval.number); return tNUMBER; } [a-zA-Z][a-zA-Z0-9]* { yylval.string = (char*)malloc (yyleng+1); strcpy (yylval.string, yytext); return tIDENTIFIER; }

8 Handling In Yacc. Within the Yacc semantic actions the $n pseudo- variables are used as before. –The %type and %token declarations have already defined the types associated so the correct union type is automatically used. identList : tIDENTIFIER { printf (“Id %s declared.\n”, $1); } expression : tNUMBER { $$ = $1; } | expression '+' expression { $$ = $1 + $3; }

9 Identifiers. Identifiers are user-defined names and may denote a variety of entities depending on the language: –constants; –variables; –functions; –… … … We will consider only their use as variables here.

10 The Symbol Table. The parser requires to keep track of –which identifiers have been introduced into the user's program; and –what attributes these identifiers have. The symbol table is the data structure used for this task; –a collection of symbol table entries; –one entry for each identifier; –each entry containing the identifier name and relevant attributes.

11 A Symbol Table API Header. int isDeclared (char *id); int addId (char *id); int getValue (char *id, double *v); int setValue (char *id, double v);

12 Symbol Table Entries. An entry in the symbol table represents the meaning of a single identifier. The attributes of this entry will reflect this meaning; for example –name, value (GenVal); –name, type, memory address (typical variable); –name, return type, no. of parameters, parameter types (function/method); –… … …

13 Example Entry. struct entry { char *name; double value; struct entry *next; };

14 Implementing The Symbol Table. The symbol table itself can be implemented in a variety of ways: –using arrays; –a list of structs; –using STL; –… … … See the GenVal symbol table version for a very simple example of using a singly linked list of structs.See the GenVal symbol table version


Download ppt "Semantic Values and Symbol Tables © Allan C. Milne Abertay University v14.6.17."

Similar presentations


Ads by Google