Compilation Principle Program Design Practice (2) global variable definition

Source: Internet
Author: User
Tags constant definition

The code for the following sections is rewritten based on the Pascal code in appendix A of the p288 textbook. The requirements for rewriting are:

1. Use the original variable name and type name, function name, and parameter name whenever possible

2. Additional Implementation of operators or functions not provided in C ++

3. Try not to change the original processing process.

In addition, the comments in the Code are basically taken from the blog of technical expert Li fanxi. Address is http://blog.csdn.net/lifanxi/article/details/3833

I would like to extend my high respect to this tech expert!

Due to the large number of codes, this post is divided into several times. This post first contains the statements and global variable definition statements in the file.

 

/* PL/0 compile program and code generation explanation run program * // * PL/0 compiler with code generation * // * introduce necessary header files and type names */# include <set> Using STD:: Set; # include <fstream> Using STD: fstream; Using STD: ifstream; Using STD: ofstream; # include <iostream> Using STD: istream; Using STD:: CIN; Using STD: cout; Using STD: Endl; # include <iomanip> Using STD: SETW; Using STD: Left; Using STD: boolalpha; # include <cstring> # include <cctype>/* constant definition */const int norw = 13; /* of reserved words * // * Number of reserved words */const int txmax = 100;/* length of identifier table * // * length of the identifier table (capacity) */const int Nmax = 14;/* max number of digits in numbers * // * Maximum number of digits */const int Al = 10; /* length of identifiers * // * Maximum length of the identifier */const int amax = 2047;/* maximum address * // * addressing space */const int levmax = 3; /* max depth of block nesting * // * Maximum number of nested block layers allowed */const int cxmax = 200; /* size of code array * // * class pcode target code array length (can accommodate the number of lines of code) * // * type definition */Enum symbol {NUL, ident, number, plus, minus, times, slash, oddsym, eql, NEQ, LSS, Leq, gtr, geq, lparen, rparen, comma, semicolon, period, becomes, beginsym, endsym, ifsym, thensym, whilesym, writesym, readsym, dosym, callsym, constsym, varsym, procsym};/* symobl identifies different types of words */typedef char Alfa [Al]; /* The Alfa type is used for the identifier */Enum object {constant, variable, procedure};/* The object type is three identifiers */typedef set <symbol> symset; /* symset is a collection type of the symbol type. It can be used to store a set of symbol */Enum FCT {values, opr, lods, sto, Cal, _ int, JMP, JPC }; /* FCT types identify each instruction for pcode */struct instruction {fct f;/* Function Code */int l;/* level */int; /* displacement ADDR */};/* class pcode command type, which contains three fields: instruction F, layer difference l, and another operand A * // * limit 0, A load constant a opr 0, a execute opr a d3l, A Load Variable L, A sto l, a store variable l, a Cal l, A call procedure a at Level L int 0, a increment T-register by a JMP 0, a jump to a JPC 0, A jump conditional to a * // * global variable definition */ofstream fa;/* The text file fa is used to list the source program */ofstream fa1, fa2; /* The text file fa1 is used to list the class pcode code, and fa2 is used to record the process of interpreting the class pcode Code */bool listswitch; /* true set list object code * // * If this variable is set to true, the program will list the class pcode code after compilation; otherwise, the class pcode code will not be listed */Char ch; /* Last char read * // * is mainly used by the lexical analyzer to store the last character */Symbol sym read from the file; /* Last symbol read * // * the output result of the lexical analyzer, which stores the type of the token most recently recognized */Alfa ID; /* Last identifier read * // * lexical analyzer output result, stores the name of the last recognized identifier */INT num; /* Last number read * // * lexical analyzer output result, stores the value of the last recognized Number */INT cc; /* Character Count * // * row buffer column pointer */INT ll;/* line length * // * row buffer length */int kk; /* this variable is introduced for program performance considerations. For details, see the getsym process annotation */int cx;/* Code Allocation Index * // * Code allocation pointer, the code generation module always generates new Code */Char line [81] at the position indicated by CX;/* row buffer, used to read a row from the file, */Alfa A is used to obtain words during confession analysis;/* the lexical analyzer temporarily stores the words being analyzed */instruction code [cxmax]; /* The generated pcode-like table stores the compiled pcode-like code */Alfa word [norw];/* reserved word table */Symbol wsym [norw]; /* The symbol type corresponding to each reserved word in the reserved word table */Symbol ssym ['^'-'+ 1]; /* Tables of symbol types corresponding to some symbols */Char mnemonic [8] [5];/* Tables of class pcode instructions */symset declbegsys, statbegsys, and facbegsys; /* declaration start, expression start, and item start Symbol Set * // * symbol table */struct {Alfa name;/* symbol name */object kind; Union {int val; /* if it is the constant value in the constant name Val */struct {int level, ADR, size;/* if it is a variable name or process name, storage layer difference, offset address, and size */};} table [txmax + 1]; ifstream fin;/* the fin text file is used to point to the input source program file, the fout program does not use */Alfa fname;/* file name for storing the PL/0 source program file */INT err;/* Total number of errors */

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.