📄 pas.l
字号:
%{/****************************************************************************** LEXICAL ANALYSER for ISO standard Pascal ----------------------------------------This lexical analyser satisfies the following requirements in BritishStandards Institution Specification for Computer programming language PascalBS6192:1982 (ISO 7185:1983)6.1.1 6.1.2 6.1.3 6.1.4 6.1.5 6.1.6 6.1.76.1.8 (except ^Z is considered to be a space)6.1.9Downloaded from comp.compilers ftp site.-------------------Modifcations :------------------- 961105 cartegw@humsci.auburn.edu Gerald Carter modified file to generate parser for Compx Timing Tool******************************************************************************/// INCLUDE FILES#include <iostream.h>#include <fstream.h>#include <stdio.h>#include <ctype.h>#include <string.h>#include "symbol.h"#include "syntax.h"#include "maple.h"#include "pas.tab.h"#include "pas.h"#include "stack.h"// GLOBAL VARIABLESint error_count;symbolTable sTable;int key_token;char numbertext[80];char lastident[128];int character_no = -1;symbolNode *last_id_token = NULL;syntaxNode *source_tree;mapleNode *maple_expr;stack<mapleNode*> maple_stack;static int DebugLevel = 1;// function declarations local to this fileint IsReserved ( char* );int strings ( void );int comment ( void );int to_lower ( char* ); // pas.y extern int yyparse( );// gen_maple.ccextern int GenerateMapleExpression (syntaxNode*, stack<mapleNode*>&);%}/* ------------------------------------------------------------------ *//* FLEX DECLARATIONS */comment_style1 "{"[^}]*"}"comment_style2 "(*"([^*]*")"|"*"*[^)])*"*)"char_literal [a-zA-Z][a-zA-Z0-9_]*/* ------------------------------------------------------------------ *//* FLEX RULES */%%\n ;[ \t\32] ;{char_literal} { if ((key_token=IsReserved(yytext)) == IDENTIFIER) { last_id_token = sTable.Insert (symbolNode(yytext)); return (IDENTIFIER); } else return (key_token); }"<=" { return (LE); }">=" { return (GE); }"<>" { return (NE); }":=" { return (BECOMES); }".." { return (DOTDOT); }"(." { return ('['); }".)" { return (']'); }"-" |"+" |"*" |"/" |"=" |"<" |">" |"(" |")" |"[" |"]" |"." |"," |";" |":" |"^" { return (yytext[0]); }"@" { return ('^'); }[0-9]+("."[0-9]+)?[eE][+-]?[0-9]+ |[0-9]+"."[0-9]+ { strcpy (numbertext,yytext); last_id_token = sTable.Insert (symbolNode(yytext)); return (UNSIGNED_REAL); }[0-9]+ { strcpy (numbertext,yytext); last_id_token = sTable.Insert (symbolNode(yytext)); return (UNSIGNED_INT); }{comment_style1} ;{comment_style2} ;"\'".*"\'" { return (STRING); }. { char m[40]; sprintf (m,"Illegal character '\\%o'", (int)yytext[0]); yyerror (m); }%%//**********************************************************************// USER CODE SECTION//**********************************************************************// --------------------------------------------------------------------// MAIN DRIVER FOR COMPILED VERSION// --------------------------------------------------------------------int main (int argc, char* argv[]){ // Local variables FILE *flptr; int i, count = 0; char indent[256] = ""; // For debugging the bison parser function // yydebug = 1; // declare and open the output streams for the symbol table, // syntax tree and maple expression tree ofstream symbolOS ("compx.sym"); if (symbolOS == NULL) { cerr << "Unable to open symbol table output stream!\n"; exit ( -1 ); } ofstream syntaxOS ("compx.gml"); if (syntaxOS == NULL) { cerr << "Unable to open syntax tree output stream!\n"; exit ( -1 ); } ofstream mapleOS ("compx.mpl"); if (mapleOS == NULL) { cerr << "Unable to open maple expression output stream!\n"; exit ( -1 ); } // Parse the source file (or standard input) if (argc == 1) yyparse (); else { for (i = 1; i < argc; i++) { if ((flptr = freopen(argv[i], "r",stdin)) == NULL) cerr << argv[0] << ": Can't open " << argv[i] << "\n"; else { if (argc > 2) fprintf(stderr, "%s:\n", argv[i]); yylineno = 1; yyparse (); } } } if (error_count) { cerr << error_count << " syntax error(s) detected\n"; exit(-1); } else cerr << "No syntax errors detected\n"; // Print the symbol Table symbolOS << "\n\nSymbol Table for Pascal source code...\n" << "-----------------------------------\n" << sTable << "\n"; symbolOS.close (); // After the call to yyparse, we should have the syntax tree // in the soureTree variable. Now generate the maple expression // tree. source_tree -> PrintGML ( syntaxOS ); syntaxOS.close (); if ( GenerateMapleExpression (source_tree, maple_stack) != 0 ) { cerr << "FATAL : error generating the maple expression." << endl << "Program exiting." << endl; exit ( -1 ); } // The root of the maple expression tree should be the last // thing on the stack. If not then an error occurred!!!!!! if ( ! maple_stack.IsEmpty() ) { maple_expr = maple_stack.Pop (); if ( DebugLevel >= 2 ) maple_expr -> PrintGML ( mapleOS ); if ( ! maple_stack.IsEmpty () ) { if ( DebugLevel >= 1 ) // Print the remaining values on the stack cerr << "*\n\nMaple Stack remaining after root popped!!!\n"; while ( ! maple_stack.IsEmpty () ) { if ( DebugLevel >= 2 ) { cerr << "---" << endl; maple_stack.Print ( cerr ); cerr << "---" << endl; cerr << "*************" << endl; } mapleNode* ptr = 0; ptr = maple_stack.Pop (); ptr->PrintGML ( mapleOS ); delete ptr; } } } else { maple_expr = 0; cerr << "No maple expression tree generated.\n"; } // Now we are ready to print the Maple Expression. The order // will go like this... // 1. Parse the symbol table inorder and print out // any functions or procedures that are found. // 2. Print out the maple expression for the program. if ( maple_expr != 0 ) { // Step #1 sTable.PrintProcExpr ( mapleOS ); // Step #2 mapleOS << "assign ( rsolve ( f(n)="; maple_expr -> PrintTree ( mapleOS ); mapleOS << ", f(n) ) );\n"; } // Close the maple expression output stream mapleOS.close (); // successful completion return (0);} //************** end of main () ********************// ********************************************************************// * Keyword// *// * The following is a table of structures with an entry for each// * keyword in ISO Pascal. The struct contains the keyword and the// * token value for that keyword as defined in pas_tab.h// *typedef struct { char* rw; int rwv;} reservedTableEntry;# define NUM_RESERVED 36reservedTableEntry reserved_tab[NUM_RESERVED] = { {"and", AND_TOK}, {"array", ARRAY}, {"begin", SBEGIN}, {"case", CASE}, {"const", CONST}, {"div", DIV}, {"do", DO}, {"downto", DOWNTO}, {"else", ELSE}, {"end", END}, {"file", SFILE}, {"for", FOR}, {"forward", FORWARD}, {"function", FUNCTION}, {"goto", GOTO}, {"if", IF}, {"in", IN}, {"label", LABEL}, {"mod", MOD}, {"nil", NIL}, {"not", NOT_TOK}, {"of", OF}, {"or", OR_TOK}, {"packed", PACKED}, {"procedure", PROCEDURE}, {"program", PROGRAM}, {"record", RECORD}, {"repeat", REPEAT}, {"set", SET}, {"then", THEN}, {"to", TO}, {"type", TYPE}, {"until", UNTIL}, {"var", VAR}, {"while", WHILE}, {"with", WITH} };// ********************************************************************// * to_lower ()// *to_lower(char *str){ char * cp; for (cp=str; *cp; cp++) if (isupper(*cp)) *cp -= ('A' - 'a') ;}// ********************************************************************// * IsReserved()// *// * This function lloks up the 'str' parameter in the reserved word// * array using a binary search.int IsReserved (char *str){ int min; int max; int guess, compare; min = 0; max = NUM_RESERVED-1; guess = (min + max) / 2; to_lower ( str ); for (guess=(min+max)/2; min<=max; guess=(min+max)/2) { if ((compare = strcmp(reserved_tab[guess].rw, str)) < 0) min = guess + 1; else if (compare > 0) max = guess - 1; else return reserved_tab[guess].rwv; } return IDENTIFIER;}// ###################################################################// ## 961105 cartegw@humsci.auburn.edu Gerald Carter// ## The following functions were pasrt of the original lex file. They// ## are currently not needed and have been commented out./*int input(){ if(yysptr > yysbuf) { yytchar = U(*--yysptr); } else yytchar = getc(yyin); character_no++; if(yytchar == 10) yylineno ++; if(yytchar == 0) yytchar= ' '; if(yytchar == EOF) yytchar= 0; return yytchar;}unput(int c){ yytchar = c; if (yytchar == '\n') yylineno--; *yysptr++ = yytchar; character_no--;} *///********** end of pascal.l ******************************************//*********************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -