Expanded C0 grammar compiler Development Notes (1) symbol table, c0 Grammar

Source: Internet
Author: User
Tags constant definition

Expanded C0 grammar compiler Development Notes (1) symbol table, c0 Grammar

Introduction

This is a compilation task.

1. Expand C0 Grammar

 Grammar includes constant description and definition, variable description and definition, no return value function definition and call, Return Value Function Definition and call, Condition Statement, while loop statement, Condition Statement, value assignment statement, and return statements, read statements, and write statements, supports addition, subtraction, multiplication, division, and integer comparison. Contains a one-dimensional array, real type, and for loop statement. The program is entered by the main method. For more information about the syntax, see:

1 <addition operator >::=+ |-2 <multiplication operator >::=* |/3 <relational operator> :: ==<|<=|>|>=|! = | = 4 <letter >::=_ | a |... | z | A |... | Z 5 <number >::= 0 | <non-zero number> 6 <non-zero number >::= 1 |... | 9 7 <character >::= '<addition operator>' | '<multiplication operator>' | '<letter>' | '<number> '8 <string> :: = "{ASCII character of 32, 33, 35-126 in decimal format}" 9 <program> :: = [<constant description>] [<variable description>] {<function definition with return value >|< function definition without return value >}< main function> 10 <constant description> :: = const <constant definition >;{ const <constant definition >;}11 <constant definition >::= int <identifier >=< integer> {, <identifier >=< integer >}12 | char <identifier >=< character >{, <identifier >=< character >}13 <unsigned integer> :: = <non-zero number >{< number >}14 <integer >::= [+ |-] <unsigned integer >|015 <identifier> :: = <letter >{< letter >|< number >}16 <declaration header >::= int <identifier >| char <identifier> 17 <variable description> :: = <variable definition >;{< variable definition >;}18 <variable definition> :: = <type identifier> (<identifier >|< identifier> '[' <unsigned integer> ']') {, (<identifier >|< identifier> '[' <unsigned integer> ']')} 19 <constant> :: = <integer> | <character> 20 <type identifier >::= int | char21 <Return Value Function Definition >:=< declaration header> '(' <parameter> ') ''{'<compound statement>'} '22 <No Return Value Function Definition >::= void <identifier> '(' <parameter> ') '{' <compound statement> '}' 23 <compound statement >::= [<constant description>] [<variable description>] <statement column> 24 <parameter>: :=< parameter table> 25 <parameter table >::=< type identifier> <identifier >{, <type identifier >}| <null> 26 <main function> :: = void main' ('')'' {'<compound statement>'} '27 <expression> :: = [+ |-] <item >{< addition operator> <item >}28 <item> :: = <factor >{< multiplication operator> <factor >}29 <factor> :: = <identifier> | <identifier> '[' <expression> ']' | <integer> | <character> | <return value function call Statement> | '(' <expression> ') '30 <statement >::= <Condition Statement >|< loop statement >|' {'<statement column>'} '| <return value function call Statement>; 31 | <no return value function call statement >;|< value assignment statement >;|< read Statement >;|< Write statement >;|< null>; | <Condition Statement> | <Return Statement>; 32 <value assignment statement> :: = <identifier >=< expression >|< identifier> '[' <expression> ']' = <expression> 33 <Condition Statement> :: = if '(' <condition> ')' <Statement> [else <Statement>] 34 <condition> :: = <expression> <relational operator> <expression> | <expression> // if the expression is 0, the condition is false. Otherwise, the condition is true. <circular Statement> :: = while '(' <condition> ')' <Statement> 36 <Condition Statement >:= switch '(' <expression> ') ''{'<condition table>'} '37 <condition table >::= <condition substatement >{< condition substatement >}38 <condition substatement> :: = case <constant >:< Statement> 39 <return value function call statement >::= <identifier> '(' <value parameter table> ') '40 <function call statement without return value >:=< identifier> '(' <value parameter table> ') '41 <value parameter table >:=< expression> {, <expression >}| <null> 42 <statement column >::{< statement >}43 <read Statement >:= scanf' ('<identifier> {, <identifier >}') '44 <Write statement >:: = printf' ('<string >,< expression>') '| printf' (' <string> ') '| printf' (' <expression> ') '45 <return Statement >::= return [' ('<expression>') '] 46 47 48 additional instructions: 49 50 (1) char expression, which uses an integer corresponding to the ASCII code of the character for calculation. In the Write statement, the output character 51 52 (2) the identifier is not case sensitive. The string in the Write statement 53 54 (3) is output 55 56 (4, the expression after the switch and the constant after the case only allow int and char Types. In each case, after the sub-statement is executed, the sub-statement 57 58 (5) the subscript of the array starts from 0.Expanded C0 Grammar

Ii. symbol Table)

A symbol table is a table in which a program is compiled to record the characteristics of various names (I .e. identifiers) in the source program. Each symbol table entry in the symbol table is filled with the name identifier and information associated with the name. This information fully reflects the attributes of each symbol and their features during compilation, for example, the type (array, variable, constant, etc.), type (integer, struct type, etc.), value, and other information related to the semantics of the name.

1. symbol table structure

The symbol table consists of the symbol table items (TableItem). For the structure of each symbol table item, see:

1 class Table; 2 class TableItem 3 {4 public: 5 string name; // symbol Table item name, such as function name, variable name, etc. 6 int kind; // identifier type, 7 int types such as VAR, CONST, ARRAY, and FUNCTION; // 8 string values such as INT and CHAR; // value or address (such as constant value and variable address) 9 int dimension; // the upper bound of the array and the number of function parameters 10 Table * pChildTable; // point to the child Table 11 Table * pParentTable derived from this item; // point to the Table 12 13 TableItem (string name, int kind, int type, string value, int dimension, table * parentTable ); 14 ~ TableItem (); 15 };Symbol table item Structure

2. symbol table management

Symbol tables are managed by the symbol table management class (TableManager) as follows:

1 class Table; 2 class TableItem; 3 class TableManager 4 {5 public: 6 Table * pCurrentTable; // point to the current symbol Table 7 Table * pTopTable; // point to the top-level symbol table 8 TableItem * pCurrentItem; // point to the current symbol table Item 9 10 TableManager (); 11 virtual ~ TableManager (); 12 13 // check whether there is a 14 int checkCurrent (string name, int isCaseSensitive) symbol table item with the name in the current symbol table ); 15 // check whether all symbol tables with name exists 16 int checkAll (string name, int isCaseSensitive ); 17 // return the corresponding symbol table Item 18 TableItem * find (string name, int isCaseSensitive) according to the name; 19 // insert symbol table item 20 void insert (string name, int kind, int type, string value, int dimension); 21 // The current symbol table item output stack 22 void pop (); 23 // press the symbol table item corresponding to name to stack 24 void push (string name); 25 };Symbol table management class

3. symbol table management algorithm

(1) stack compression algorithm: symbol table stack compression operation. If the last item of the current symbol table is the function header or process header, a sub-symbol table is created for this item, update the pointer of the current symbol table to this symbol table.

1 void TableManager: push (string name) 2 {3 if (pCurrentTable-> itemNumber = 0) 4 {5; // if there is no symbol table item currently, DONOTHING 6} 7 // if the symbol table item named name has a derived table, press the table to the top of the stack 8 else if (find (name, 1)-> pChildTable! = NULL) 9 {10 pCurrentTable = find (name, 1)-> pChildTable; 11} 12 else13 {14; // if this symbol table item does not have a child table, DONOTHING15} 16}Push (string name)

(2) Out-of-Stack Algorithm: symbol table rollback operation, which updates the current symbol table to the sub-stack top symbol table

1 void TableManager: pop () 2 {3 // if the current symbol table is derived from a symbol table entry, update the current symbol table to the symbol table where the symbol table item is located. 4 if (pCurrentTable-> pParentItem! = NULL) 5 {6 pCurrentTable = pCurrentTable-> pParentItem-> pParentTable; 7} 8 else 9 {10; // if it is a top-level symbol table, DONOTING11} 12}Pop ()

(3) query algorithm: modified from binary search (symbol table items are stored in an array in alphabetical order of their names, and binary search is performed in the array ), if this symbol table item exists, the index value is returned; otherwise, the position where the symbol table item should be inserted is returned.

1 int Table: find (string name, int left, int right, int isCaseSensitive) 2 {3 int mid = (left + right)/2; 4 if (left = right) 5 {6 if (compare (name, itemList [itemIndex [left]-> name, isCaseSensitive) = 0) 7 {8 itemExist = 1; 9 return left; 10} 11 else if (compare (name, itemList [itemIndex [left]-> name, isCaseSensitive) = 1) 12 {13 itemExist = 0; 14 return left + 1; 15} 16 else17 {18 itemExist = 0; 19 return left; 20} 21} 22 else if (left + 1 = right) 23 {24 if (compare (name, itemList [itemIndex [left]-> name, isCaseSensitive) = 0) 25 {26 itemExist = 1; 27 return left; 28} 29 else if (compare (name, itemList [itemIndex [right]-> name, isCaseSensitive) = 0) 30 {31 itemExist = 1; 32 return right; 33} 34 else35 {36 itemExist = 0; // return right 37 in the middle of left and right in newItem; 38} 39} 40 else if (compare (name, itemList [itemIndex [mid]-> name, isCaseSensitive) =-1) 41 {42 return find (name, left, mid, isCaseSensitive); 43} 44 else if (compare (name, itemList [itemIndex [mid]-> name, isCaseSensitive) = 1) 45 {46 return find (name, mid, right, isCaseSensitive); 47} 48 else49 {50 itemExist = 1; 51 return mid; 52} 53}Find (string name, int left, int right, int isCaseSensitive)

(4) Insertion Algorithm: Insert new symbol table items into the symbol table, and create indexes in alphabetical order of the names. The new entry is saved into an ordered array for binary search.

1 int Table: insert (string name, int kind, int type, string value, int dimension) 2 {3 newItem = new TableItem (name, kind, type, value, dimension, this); 4 if (itemNumber = 0) 5 {6 itemIndex [0] = 0; 7 itemList [itemNumber ++] = newItem; 8 return 0; 9} 10 else11 {12 if (name <itemList [itemIndex [0]-> name) 13 {14 for (int I = itemNumber-1; I> = 0; I --) 15 itemIndex [I + 1] = itemIndex [I]; 16 itemIndex [0] = itemNumber; 17 itemList [itemNumber ++] = newItem; 18} 19 else if (name> itemList [itemIndex [itemNumber-1]-> name) 20 {21 itemIndex [itemNumber] = itemNumber; 22 itemList [itemNumber ++] = newItem; 23} 24 else25 {26 int index = find (newItem-> name, 0, itemNumber-1, 0); 27 if (itemExist = 1) 28 {29 // ERROR30 return-1; 31} 32 else33 {34 for (int I = itemNumber-1; I >= index; I --) 35 itemIndex [I + 1] = itemIndex [I]; 36 itemIndex [index] = itemNumber; 37 itemList [itemNumber ++] = newItem; 38} 39} 40 return 0; 41} 42}Insert (string name, int kind, int type, string value, int dimension)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.