编译原理课程设计

上传人:fgh****35 文档编号:180630700 上传时间:2023-01-07 格式:DOC 页数:13 大小:49.50KB
返回 下载 相关 举报
编译原理课程设计_第1页
第1页 / 共13页
编译原理课程设计_第2页
第2页 / 共13页
编译原理课程设计_第3页
第3页 / 共13页
点击查看更多>>
资源描述
编译原理课程设计一、使用词法分析,语法分析将布尔表达式转换为逆波兰式二、LL(1)语法分析程序设计要求 (1)对输入文法,它能判断是否为LL(1)文法,若是,则转(2);否则报错并终止; (2)输入已知文法,由程序自动生成它的LL(1)分析表; (3)对于给定的输入串,应能判断识别该串是否为给定文法的句型。三、设计题目:词法自动机设计要求:用C语言或其它高级语言对PASCAL子集编制一个一遍扫描的小型编译程序对词法分析,完成识别语言单词的任务。对语法分析,若输入串是文法的句子,则输出语法分析成功,否则,给出错误的行号,错误的性质。语法描述:文法: begin end ; | | := if then while do + | * | / | | ( ) | | | = | = | a | b | c | . | x | y | z 0 | 1 | 2 | . | 9 , | ; | ( | ) | .四、设计题目:有限自动机的运行设计目的: 1、 理解有限自动机的作用 2、 利用转态图和状态表表示有限自动机 3、 以程序实现有限自动机的运行过程设计内容:(注:题目详细要求) 利用状态表和有限自动机的运行原理编制程序,使得程序能够识别一个输入串是否为一个有效的符号串,具体可以选择下面之一:无符号定点实数、自然数、整数、十六进制数或其它自己定义的符号串。设计思想: 本程序实现对无符号定点实数的判断,正确接受,否则不接受。 本程序的关键在状态表和缓冲区的运用。首先定义了一个布尔型函数ReadALine把输入的字符串送到缓冲区中;然后定义了布尔型函数Run和Getchar实现对输入字符串的正确性判断,更改Run函数可以改变程序功能:如可将状态表改变成识别“偶数”的有限自动机的状态表。 附录:(完整代码) 有限自动机#include#include/状态表相关存储信息:#defineSTATE_NUMBER4/状态数目#defineCHAR_NUMBER2/输入字符的种类:d和.#defineDIGIT0/输入数字在状态表中位于第0列/State为状态表,以整数组形式存放,0,1,2,3表示状态,-1表示没有此状态intStateSTATE_NUMBERCHAR_NUMBER=1,-1,1,2,3,-1,3,-1;intQSTATE_NUMBER=0,1,0,1;/终态标志:0非终态,1终态。/缓冲区:/输入缓冲区:由专门函数操作(ReadALine(),GetChar())#defineBUFFER_SIZE1000/表达式缓冲区大小charBufferBUFFER_SIZE;/表达式缓冲区,以0表示结束intipBuffer=0;/表达式缓冲区当前位置序号charch;/存放取得的一个字符/函数声明:boolRun();/对存储在缓冲区的一行字符串(以#结束)进行运行voidInit();/全局初始化boolReadALine();/从键盘读一行(没有空格),存于表达式缓冲区Buffer中charGetChar();/从缓冲区取一个字符,返回该字符的同时将它存于全局变量ch中/主程序:voidmain()Init();while(ReadALine()/读一行成功,对它进行判断if(Run()/对该行进行运行,看是否能被接受?printf(接受nn);elseprintf(不接受nn);/对存储在缓冲区的一行字符串(以#结束)进行运行/返回:如果是无符号定点实数,返回true;否则返回:falseboolRun()intS=0;/S存放运行时的当前状态,目前为初态while(GetChar()!=#)if(ch=0&ch=9)S=StateSDIGIT;/将状态转换成输入后的状态else/其他都为非法字符returnfalse;if(S=-1)/处于非法状态returnfalse;/运行结束,判断S是否为终态if(QS=1)/终态returntrue;else/非终态returnfalse;/全局初始化voidInit()/好像无需初始化printf(程序功能:输入一个字符串,判断它是否是a。n);printf(=nn);/从键盘读一行(没有空格),存于表达式缓冲区Buffer中,以#结束,并置ipBuffer=0;/读到非空字符串:返回true;读到单独的“#”:返回falseboolReadALine()intl;printf(请输入以#号结束的无空格字符串:);scanf(%s,Buffer);l=strlen(Buffer);/读入的字符串的长度if(l=0)returnReadALine();/输入了空字符串,重新输入if(Buffer0=#)returnfalse;/输入单独的#表示不再输入Bufferl=#;/最后一个字符用结束标记#代替(本来是0)ipBuffer=0;/初始化缓冲区指针returntrue;/从缓冲区取一个字符,返回该字符的同时将它存于全局变量ch中/成功:返回字符;不成功:返回#charGetChar()if(ch=BufferipBuffer)!=#)ipBuffer+;returnch;附录:词法自动机。import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import java.io.*;import java.util.*;class frame extends JFrame implements ActionListenerJButton button1=new JButton(打开现有文件);JButton button2=new JButton(现场输入文件);Choice ch=new Choice();JButton button=new JButton(执行);TextArea ta=new TextArea();TextArea tb=new TextArea();JFileChooser chooser=new JFileChooser();FileInputStream readfile=null;int b;byte buffer=new byte25000;String str=null;String id=asm,cout,main,FALSE,TRUE,include,boolean,default,float,operator,abs,static_cast,union,auto,delete,for,private,struct,unsigned,bool,do,friend,protected,switch,using,break,double,goto,public,template,virtual,case,dynamic_cast,if,register,this,void,catch,else,inline,reinterpret_cast,throw,volatile,char,enum,int,return,true,wchar_t,class,explicit,long,short,try,while,const,export,mutable,signed,typedef,const_cast,extern,namespace,sizeof,typeid,Continue,false,new,static,typename;FileReader file=null;BufferedReader in=null;File f=null;frame()super();Container c=this.getContentPane();ch.add(C语言常数识别);ch.add(C语言标识符识别);ch.add(C语言注释字母大写);ch.add(C语言保留字大写);c.setLayout(null);c.add(button1);button1.setBounds(5,5,120,25);button1.addActionListener(this);c.add(button2);button2.setBounds(130,5,120,25);button2.addActionListener(this);c.add(ch);ch.setBounds(260,5,120,25);c.add(button);button.setBounds(390,5,80,25);button.addActionListener(this);c.add(ta);ta.setBounds(5,40,482,200);ta.setEditable(false);c.add(tb);tb.setBounds(5,250,482,200);tb.setEditable(false);this.setBounds(300,100,500,500);this.setVisible(true);this.setResizable(false);this.addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(0););public void actionPerformed(ActionEvent ae)/-输入原文件-if(ae.getSource()=button1)String s;ta.setText(null);int state=chooser.showOpenDialog(null);f=chooser.getSelectedFile();if(f!=null&state=JFileChooser.APPROVE_OPTION)tryreadfile=new FileInputStream(f);catch(IOException ee)tryb=readfile.read(buffer,0,25000);str=new String(buffer,0,b);ta.append(str);catch(IOException e1)System.out.println(File read error);/-输入原文件-else if(ae.getSource()=button2)frame.this.ta.setEditable(true);else if(ae.getSource()=button)/-C语言常数识别-if(ch.getSelectedIndex()=0)tb.setText(null);StringTokenizer sign=new StringTokenizer(str, ()=#;n);int n=sign.countTokens();while(sign.hasMoreTokens()String s=sign.nextToken();int n1=s.length();String s1=s.substring(0,1);byte d=s1.getBytes();if(d0=48)|d0=45)int j=0;for(int i=1;in1;i+)String ss=s.substring(i,i+1);byte f=ss.getBytes();if(f0=48)j+=1;if(j=n1-1)tb.append(s+n);if(d0=48)|d0=45)int j=0;for(int i=1;in1;i+)String ss=s.substring(i,i+1);byte f=ss.getBytes();if(f0=48)|f0=46)j+=1;if(j=n1-1)tb.append(s+n);if(d0=39)int j=0;for(int i=1;in1;i+)String ss=s.substring(i,i+1);byte f=ss.getBytes();if(f0=39|(f0=97)|(f0=65)j+=1;if(j=n1-1)tb.append(s+n);elseString t=TRUE;String f=FALSE;if(s.equals(t)|s.equals(f)tb.append(s+n);/-C语言常数识别-/-C语言标志符识别-else if(ch.getSelectedIndex()=1)tb.setText(null);StringTokenizer sign=new StringTokenizer(str, ()=#;n);int n=sign.countTokens();while(sign.hasMoreTokens()String s=sign.nextToken();int n1=s.length();String s1=s.substring(0,1);byte d=s1.getBytes();if(d0=97)|(d0=65)|d0=95)int j=0;for(int i=1;in1;i+)String ss=s.substring(i,i+1);byte f=ss.getBytes();if(f0=97)|(f0=65)|(f0=95)|(f0=48)j+=1;if(j=n1-1)int m=0;for(int k=0;kid.length;k+)if(s.equals(idk)m+;if(m=0)tb.append(s+n);/-C语言标志符识别-/-C语言注释大写-else if(ch.getSelectedIndex()=2)tb.setText(null);tryfile=new FileReader(f);in=new BufferedReader(file);catch(FileNotFoundException er)catch(IOException er)String s;trywhile(s=in.readLine()!=null)if(s.startsWith(/*)while(s.indexOf(*/)=-1)s=s+in.readLine();for(int i=1;i=97&f0=122)s=s.toUpperCase();tb.append(s+n);elseif(s.startsWith(/)for(int j=1;j=97&d0=122)s=s.toUpperCase();tb.append(s+n);elseif(!(s.indexOf(/)=-1)s=s.substring(s.indexOf(/);for(int k=1;k=97&g0=122)s=s.toUpperCase();tb.append(s+n);catch(IOException ext)/-C语言注释大写-/-C语言保留字大写-else if(ch.getSelectedIndex()=3)tb.setText(null);tryfile=new FileReader(f);in=new BufferedReader(file);catch(FileNotFoundException er)catch(IOException er)String s;trywhile(s=in.readLine()!=null)if(s.indexOf(/)=-1)StringTokenizer o=new StringTokenizer(s, #()*+-/!);while(o.hasMoreTokens()String s1=o.nextToken();int m=0;for(int i=1;iid.length;i+)if(s1.equals(idi)m+;if(!(m=0)s1=s1.toUpperCase();tb.append(s1+n);elseif(!(s.indexOf(/)=-1)s=s.substring(0,s.indexOf(/);StringTokenizer o=new StringTokenizer(s, #()*+-/!);while(o.hasMoreTokens()String s1=o.nextToken();int m=0;for(int i=1;iid.length;i+)if(s1.equals(idi)m+;if(!(m=0)s1=s1.toUpperCase();tb.append(s1+n);catch(IOException ext)/-C语言保留字大写-public class Designpublic static void main(String args)new frame();
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 机械制造 > 工业自动化


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!