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

Slides:



Advertisements
Similar presentations
SYMBOL TABLES &CODE GENERATION FOR EXECUTABLES. SYMBOL TABLES Compilers that produce an executable (or the representation of an executable in object module.
Advertisements

Structure of a YACC File Has the same three-part structure as Lex Each part is separated by a % symbol The three parts are even identical: – definition.
Compiler construction in4020 – lecture 2 Koen Langendoen Delft University of Technology The Netherlands.
1 Structures. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc) and other.
Lex -- a Lexical Analyzer Generator (by M.E. Lesk and Eric. Schmidt) –Given tokens specified as regular expressions, Lex automatically generates a routine.
Chapter 8 Intermediate Code Generation. Intermediate languages: Syntax trees, three-address code, quadruples. Types of Three – Address Statements: x :=
Converting NFAs to DFAs. NFA to DFA: Approach In: NFA N Out: DFA D Method: Construct transition table Dtran (a.k.a. the "move function"). Each DFA state.
 Lex helps to specify lexical analyzers by specifying regular expression  i/p notation for lex tool is lex language and the tool itself is refered to.
Compiler Principle and Technology Prof. Dongming LU Mar. 28th, 2014.
Yacc YACC BNF grammar example.y Other modules example.tab.c Executable
176 Formal Languages and Applications: We know that Pascal programming language is defined in terms of a CFG. All the other programming languages are context-free.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
CH4.1 CSE244 Bottom Up Translation (revisited) Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road,
Parser construction tools: YACC
Syntax Analysis – Part II Quick Look at Using Bison Top-Down Parsers EECS 483 – Lecture 5 University of Michigan Wednesday, September 20, 2006.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
Values, variables and types © Allan C. Milne v
By Sidhant Garg.  C was developed between by Dennis Ritchie at Bell Laboratories for use with the Unix Operating System.  Unlike previously.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Introduction To Yacc and Semantics © Allan C. Milne Abertay University v
Syntax Specification and BNF © Allan C. Milne Abertay University v
Lesson 10 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Using CookCC.  Use *.l and *.y files.  Proprietary file format  Poor IDE support  Do not work well for some languages.
1 YACC Parser Generator. 2 YACC YACC (Yet Another Compiler Compiler) Produce a parser for a given grammar.  Compile a LALR(1) grammar Original written.
Review: Regular expression: –How do we define it? Given an alphabet, Base case: – is a regular expression that denote { }, the set that contains the empty.
YACC Example Taken from LEX & YACC Simple calculator a = a a=10 b = 7 c = a + b c c = 17 pressure = ( ) * 16.4 $
Scanning & FLEX CPSC 388 Ellen Walker Hiram College.
Synthesis and the Parse Tree © Allan C. Milne Abertay University v
Flex: A fast Lexical Analyzer Generator CSE470: Spring 2000 Updated by Prasad.
LEX (04CS1008) A tool widely used to specify lexical analyzers for a variety of languages We refer to the tool as Lex compiler, and to its input specification.
Syntax Specification with YACC © Allan C. Milne Abertay University v
–Writing a parser with YACC (Yet Another Compiler Compiler). Automatically generate a parser for a context free grammar (LALR parser) –Allows syntax direct.
Introduction to Yacc Ying-Hung Jiang
Introduction to Lexical Analysis and the Flex Tool. © Allan C. Milne Abertay University v
The Functions and Purposes of Translators Syntax (& Semantic) Analysis.
Building The Parse Tree © Allan C. Milne Abertay University v
1 Using Yacc. 2 Introduction Grammar –CFG –Recursive Rules Shift/Reduce Parsing –See Figure 3-2. –LALR(1) –What Yacc Cannot Parse It cannot deal with.
Compiler Principle and Technology Prof. Dongming LU Mar. 26th, 2014.
ICS312 LEX Set 25. LEX Lex is a program that generates lexical analyzers Converting the source code into the symbols (tokens) is the work of the C program.
Applications of Context-Free Grammars (CFG) Parsers. The YACC Parser-Generator. by: Saleh Al-shomrani.
1 LEX & YACC Tutorial February 28, 2008 Tom St. John.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
Yacc. Yacc 2 Yacc takes a description of a grammar as its input and generates the table and code for a LALR parser. Input specification file is in 3 parts.
Scanner Generation Using SLK and Flex++ Followed by a Demo Copyright © 2015 Curt Hill.
LECTURE 11 Semantic Analysis and Yacc. REVIEW OF LAST LECTURE In the last lecture, we introduced the basic idea behind semantic analysis. Instead of merely.
More yacc. What is yacc – Tool to produce a parser given a grammar – YACC (Yet Another Compiler Compiler) is a program designed to compile a LALR(1) grammar.
Computer Programming – Grammar – data types – Variables – Keywords – Operators – decision making – Loops – Arrays – Functions – files Programming Environment.
1 Structures & Unions. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc)
YACC (Yet Another Compiler-Compiler) Chung-Ju Wu
1 Syntax Analysis Part III Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
9-December-2002cse Tools © 2002 University of Washington1 Lexical and Parser Tools CSE 413, Autumn 2002 Programming Languages
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
System Software Unit-1 (Language Processors) A TOY Compiler
A Simple Syntax-Directed Translator
Constructing Precedence Table
Tutorial On Lex & Yacc.
Chapter 4 Syntax Analysis.
Syntax Analysis Part III
CSE 3302 Programming Languages
Review: Compiler Phases:
Subject Name:Sysytem Software Subject Code: 10SCS52
Yacc Yacc.
Appendix B.2 Yacc Appendix B.2 -- Yacc.
Symbol Table 薛智文 (textbook ch#2.7 and 6.5) 薛智文 96 Spring.
A simple function.
CMPE 152: Compiler Design December 4 Class Meeting
Systems Programming & Operating Systems Unit – III
Compiler Design 3. Lexical Analyzer, Flex
Presentation transcript:

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

Agenda. Semantic Values. Handling Identifiers. Symbol Tables.

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.

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; }

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

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; }

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; }

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.

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.

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);

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); –… … …

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

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