您好,欢迎访问三七文档
编译原理实验报告二姓名:学号:班级:10级计科一班1.编写一个对于Pascal源程序的预处理程序。该程序的作用是,每次被调用时都将下一个完整的语句送进扫描缓冲区,去掉注解行,同时要对源程序列表打印。%{(*lexicalanalyzerforPascal)%}%{(*本程序通过扫描对照关键字表来识别关键字*)(*过程和函数定义*)procedurecommenteof;(*检查并返回错误输入*)beginwriteln('unexpectedEOFinsidecommentatline',yylineno);end(*commenteof*);functionupper(str:String):String;(*将字符串转换为大写*)vari:integer;beginfori:=1tolength(str)dostr[i]:=upCase(str[i]);upper:=strend(*upper*);functionis_keyword(id:string;vartoken:integer):boolean;(*检查id是否为Pascal关键字;若是,返回token中相应的tokennumber*)constid_len=20;typeIdent=string[id_len];const(*Pascal关键字表:*)(*用Pascal关键字表:*)no_of_keywords=39;keyword:array[1..no_of_keywords]ofIdent=('AND','ARRAY','BEGIN','CASE','CONST','DIV','DO','DOWNTO','ELSE','END','EXTERNAL','EXTERN','FILE','FOR','FORWARD','FUNCTION','GOTO','IF','IN','LABEL','MOD','NIL','NOT','OF','OR','OTHERWISE','PACKED','PROCEDURE','PROGRAM','RECORD','REPEAT','SET','THEN','TO','TYPE','UNTIL','VAR','WHILE','WITH');keyword_token:array[1..no_of_keywords]ofinteger=(_AND,_ARRAY,_BEGIN,_CASE,_CONST,_DIV,_DO,_DOWNTO,_ELSE,_END,_EXTERNAL,_EXTERNAL,(*EXTERNAL:2spellings(seeabove)!*)_FILE,_FOR,_FORWARD,_FUNCTION,_GOTO,_IF,_IN,_LABEL,_MOD,_NIL,_NOT,_OF,_OR,_OTHERWISE,_PACKED,_PROCEDURE,_PROGRAM,_RECORD,_REPEAT,_SET,_THEN,_TO,_TYPE,_UNTIL,_VAR,_WHILE,_WITH);varm,n,k:integer;beginid:=upper(id);(*二分法检索:*)m:=1;n:=no_of_keywords;whilem=ndobegink:=m+(n-m)div2;ifid=keyword[k]thenbeginis_keyword:=true;token:=keyword_token[k];exitendelseifidkeyword[k]thenm:=k+1elsen:=k-1end;is_keyword:=falseend(*is_keyword*);%}NQUOTE[^']%{(*规则部分*)%}%%%{varc:char;kw:integer;%}[a-zA-Z]([a-zA-Z0-9])*ifis_keyword(yytext,kw)thenreturn(kw)elsereturn(IDENTIFIER);:=return(ASSIGNMENT);'({NQUOTE}|'')+'return(CHARACTER_STRING);:return(COLON);,return(COMMA);[0-9]+return(DIGSEQ);.return(DOT);..return(DOTDOT);=return(EQUAL);=return(GE);return(GT);[return(LBRAC);=return(LE);(return(LPAREN);return(LT);-return(MINUS);return(NOTEQUAL);+return(PLUS);]return(RBRAC);[0-9]+.[0-9]+return(REALNUMBER);)return(RPAREN);;return(SEMICOLON);/return(SLASH);*return(STAR);**return(STARSTAR);-|^return(UPARROW);(*|{beginrepeatc:=get_char;casecof'}':;'*':beginc:=get_char;ifc=')'thenexitelseunget_char(c)end;#0:begincommenteof;exit;end;end;untilfalseend;[\n\t\f];return(ILLEGAL);2.合并例3-3和例3-4的状态图,设计并实现一个词法分析函数,每次返回一个单词种别和属性;考虑关键字if、then、while、do;编制主程序完成测试(输入和输出)。intcode,value;strToken:=“”;GetChar();GetBC();If(IsLetter())Beginwhile(IsLetter()orIsDigit())beginConcat();GetChar();EndRetract();Code:=Reserve();If(code=0)BeginValue:=InsertId(strToken);Return($ID,value);endelseif(IsDigit(())beginwhile(IsDigit())beginConcat();GetChar();EndRetract();value:=InsertConst(strToken);return(%INT,value);endelseif(ch=‘/’)return($DIV,-);elseif(ch=‘=’)return($ASSGIN,-);elseif(ch=‘+’)return($PLUS,-);elseif(ch=‘*’)beginGetChar();if(ch=‘*’)return($POWER,-);Retract();return($STAR,-);endelseif(ch=‘;’)return($SEMICOLON,-);elseif(ch=‘(’)return($LPLAR,-);elseif(ch=‘)’)return($RPLAR,-);elseif(ch=‘{’)return($LBRACE,-);elseif(ch=‘}’)return($RPLAR,-);elseProcError();
本文标题:编译原理实验报告二
链接地址:https://www.777doc.com/doc-4630319 .html