zoukankan      html  css  js  c++  java
  • 词法分析程序的设计与实现

    词法分析程序(Lexical Analyzer)要求:

    - 从左至右扫描构成源程序的字符流

    -  识别出有词法意义的单词(Lexemes

    -  返回单词记录(单词类别,单词本身)

    -  滤掉空格

    -  跳过注释

    -  发现词法错误

    程序结构:

    输入:字符流(什么输入方式,什么数据结构保存)

    处理:

    –遍历(什么遍历方式)

    –词法规则

    输出:单词流(什么输出形式)

    –二元组

    单词类别:

    1.标识符(10)

    2.无符号数(11)

    3.保留字(一词一码)

    4.运算符(一词一码)

    5.界符(一词一码)

    单词符号

    种别码

    单词符号

    种别码

    begin

    1

    :

    17

    if

    2

    :=

    18

    then

    3

    <

    20

    while

    4

    <=

    21

    do

    5

    <>

    22

    end

    6

    >

    23

    l(l|d)*

    10

    >=

    24

    dd*

    11

    =

    25

    +

    13

    ;

    26

    -

    14

    (

    27

    *

    15

    )

    28

    /

    16

    #

    0

    代码如下:

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    char prog[800],token[8];//程序段,单词符号
    char ch,error;
    int syn,p,m,n,sum;//单词符号类型syn,整数sum,p当前要识别程序段第一个字符指针p
    char *word[6]={"begin","if","then","while","do","end"};
    int main(){
    p=0;
    printf(" 请输入程序段:");
    do{
    ch=getchar();
    prog[p++]=ch;
    } while(ch!='#');
    p=0;

    do{
    ch=prog[p++];
    switch(ch){
    case '+':
    syn=13;token[0]=ch;
    break;
    case '-':
    syn=14;token[0]=ch;
    break;
    case '*':
    syn=15;token[0]=ch;
    break;
    case '/':
    syn=16;token[0]=ch;
    break;
    case '=':
    syn=25;token[0]=ch;
    break;
    case ';':
    syn=26;token[0]=ch;
    break;
    case '(':
    syn=27;token[0]=ch;
    break;
    case ')':
    syn=28;token[0]=ch;
    break;
    case '#':
    for(int i=0;i<8;i++)
    token[i]=NULL;
    syn=0;token[0]=ch;
    break;
    default:
    if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){
    m=0;
    while((ch>='0'&&ch<='9')||(ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){
    token[m++]=ch;
    ch=prog[p++];
    }
    token[m++]='';
    p--;
    syn=10;
    for(int i=0;i<6;i++){
    if(strcmp(word[i],token)==0){
    syn=i+1;
    break;
    }
    }

    }else if((ch>='0'&&ch<='9')){
    sum=0;
    while(ch>='0'&&ch<='9'){
    sum=sum*10+(ch-'0');
    ch=prog[p++];
    }
    p--;
    syn=11;

    }else if(ch==' '){
    ch=prog[p];
    syn=100;

    }else{
    int n=p-1;
    syn=-1;
    error=prog[n];

    }
    break;

    }
    switch(syn){
    case 100:
    break;
    case 11:
    printf(" (%d,%d)",syn,sum);
    break;
    case -1:
    printf(" 无'%c'符号!",error);
    break;
    default:
    printf(" (%d,%s)",syn,token);
    break;
    }
    }while(syn!=0);
    }

    运行截图如下:

  • 相关阅读:
    zookeeper使用场景
    zookeeper安装配置
    hadoop 远程调试
    deep learning笔记
    Sentiment Analysis(1)-Dependency Tree-based Sentiment Classification using CRFs with Hidden Variables
    PRML阅读笔记 introduction
    Python 学习笔记(2)
    python nltk 学习笔记(5) Learning to Classify Text
    python nltk 学习笔记(4) Writing Structured Programs
    python nltk 学习笔记(3) processing raw text
  • 原文地址:https://www.cnblogs.com/lywkkk/p/11654317.html
Copyright © 2011-2022 走看看