《C词法分析器设计报告》由会员分享,可在线阅读,更多相关《C词法分析器设计报告(28页珍藏版)》请在金锄头文库上搜索。
1、C#C#词法分析器设计报告词法分析器设计报告同济大学同济大学 2004 级计算机级计算机 4 班班 范成范成 040648程序界面开发环境:开发环境: Microsoft Visual Studio 2005程序运行环境:程序运行环境: 任何安装了任何安装了 Microsoft .NET Framework 2.0(或(或 2.0 以上版本)的计算机。以上版本)的计算机。 Microsoft .NET Framework 2.0 下载地址:下载地址:http:/ System.Collections.Generic;using System.ComponentModel;using Syste
2、m.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;namespace Scannerpublic partial class Scanner : Formpublic Scanner()InitializeComponent();rtbInput.SelectAll();private string token; /标识符表private int tokenIndex = 0; /标识符表指针private long constant; /整型常数表private i
3、nt constantIndex = 0; /整型常数表指针private double dblConstant; /浮点常数表private int dblConstantIndex = 0; /浮点常数表指针private string strConst; /字符串表private int strConstIndex; /字符串表指针private char charConst; /字符表private int charConstIndex = 0; /字符表指针private string input; /存放待识别的源程序字符串private int inputIndex = 0; /
4、input字符串指针private char ch; /存放最新读进的源程序字符private string strToken; /存放构成单词符号的字符串private bool scanned = false; /是否扫描过至少一次private static string tokenReserve = “abstract“, “do“, “in“, “protected“, “true“,“as“, “double“, “int“, “public“, “try“,“base“, “else“, “interface“, “readonly“, “typeof“,“bool“, “enu
5、m“, “internal“, “ref“, “uint“,“break“, “event“, “is“, “return“, “ulong“,“byte“, “explicit“, “lock“, “sbyte“, “unchecked“,“case“, “extern“, “long“, “sealed“, “unsafe“,“catch“, “false“, “namespace“, “short“, “ushort“,“char“, “finally“, “new“, “sizeof“, “using“,“checked“, “fixed“, “null“, “stackalloc“,
6、 “virtual“,“class“, “float“, “object“, “static“, “volatile“,“const“, “for“, “operator“, “string“, “void“,“continue“, “foreach“, “out“, “struct“, “while“,“decimal“, “goto“, “override“, “switch“, “default“, “if“, “params“, “this“, “delegate“, “implicit“, “private“, “throw“ ; /C#保留关键字表(77个) /将下一输入字符读到c
7、h中,搜索指示器前移一字符位置private void getChar()ch = inputinputIndex;inputIndex+;/将ch中的字符连接到strToken之后private void concat()strToken += ch;/判断ch中的字符是否为字母private bool isLetter()if (ch = a else return (strToken + “ “);elsereturn (ch + “ “);/整型常数和浮点常数else if (isDigit()if (input.Length 1)while (isDigit() if (inputI
8、ndex “);elsereturn (ch + “ “);else if (ch = =)if (inputIndex “);retract();return (“= “);else return (“= “);else if (ch = !)if (inputIndex “);retract();return (“! “);else return (“! “);else if (ch = +)if (inputIndex “);else if (ch = =) return (“+= “);retract();return (“+ “);else return (“+ “);else if
9、 (ch = -)if (inputIndex “);else if (ch = =) return (“-= “);else if (ch = ) return (“- “);retract();return (“- “);else return (“- “);else if (ch = |)if (inputIndex “);else if (ch = =) return (“|= “);retract();return (“| “);else return (“| “);else if (ch = else if (ch = =) return (“retract();return (“
10、else return (“else if (ch = )if (inputIndex = “);else if (ch = ) return (“ “);retract();return (“ “);else return (“ “);else if (ch = “);else if (ch = “);retract();return (“);else return (“);else if (ch = )if (inputIndex “;elseretract();return “INVALID USE OF “;getChar();if (ch = )ch = inputinputInde
11、x - 2;value = insertChar();return (inputinputIndex - 2 + “ “);else if (inputIndex = input.Length - 1)retract();return “INVALID USE OF “;elseretract();retract();return “INVALID USE OF “;else if (inputIndex “);else if (inputIndex = input.Length - 1)retract();return “INVALID USE OF “;elseretract();retr
12、act();return “INVALID USE OF “;else return “INVALID USE OF “;else if(ch=“)bool notMatch = true;if (inputIndex = input.Length) return (“INVALID USE OF “);else if (inputinputIndex = “)strToken = “;value = insertString();if (inputIndex “);elsegetChar();while (notMatch else if (ch = *)if (inputIndex “);
13、retract();return (“* “);else return (“* “);else if (ch = /)if (inputIndex input.Length - 2) return(“INVALID USE OF /*“);getChar();while (ch != * getChar();if (inputIndex = input.Length) stillSearch = false;if (inputIndex = input.Length)concat();getChar();return (“);else if (ch = /)if (inputIndex “);
14、elseretract();return (“/ “);else return (“/ “);else if (ch = #) return (“# “);else if (ch = :)if (inputIndex “);retract();return (“: “);else return (“: “);else if (ch = ;) return (“; “);else if (ch = ) return (“ “);else if (ch = ) return (“ “);else if (ch = ) return (“ “);else if (ch = $) return (“$
15、 “);else if (ch = %)if (inputIndex “);retract();return (“% “);else return (“% “);else if (ch = )if (inputIndex “);retract();return (“ “);else return (“ “);else if (ch = _) return (“_ “);else if (ch = ?)if (inputIndex “);retract();return (“? “);else return (“? “);else if (ch = ) return (“ “);else if (ch = ) return (“ “);else if (ch = ) return (“ “);else if (ch =