zoukankan      html  css  js  c++  java
  • 词法分析程序

    词法分析程序的功能:通过用户输入的关键字及其他保留字符,输出它本身及它的种别码
    符号与种别码对照表如下

    单词符号

    种别码

    单词符号

    种别码

    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

    用文法描述词法规则:
    <字母>->a/b/c.../z
    <数字>->1/2/3/4/5/6/7/8/9
    <标识符>->sum.....
    <关键字>->begin/if/then/while/do/end
    <运算符>->+,-,*,/,:,:=,<,<=,<>,>,>=,=
    <界符>->;|(|)|[|]|{|}
    已完成的代码如下:
    #include<stdio.h> #include<stdlib.h> #include<string.h> char word[6][5]={"begin","if","then","while","do","end"}; char word2[18][3]={"|","dd*","+","-","*","/",":",":=","<","<=","<>",">",">=","=",";","(",")","#"}; int words(char ch[][5]); int signs(char ch[][3]); main() { char ch[10]; printf("请输入保留字:"); scanf("%s",ch); words(ch); signs(ch); } int words(char ch[][5]) { while(ch==''||ch==10||ch==13||ch==9) { getchar; } if(ch>='a'&&ch<='z') { switch(ch)
    {}
    } } int signs(char ch[][3]) { while(ch==''||ch==10||ch==13||ch==9) { getchar; } if(ch>='0'&&ch<='9') { switch(ch)
    {} } }

      

  • 相关阅读:
    古典问题-兔子生兔子
    order by 执行计划索引使用不同的坑
    MybatisPlus 通用枚举无法正确取值
    Arrays.asList 使用细节
    java 生成pdf文件(易上手版)
    Mysql-tinyint使用之实际采坑记
    mysql
    mysql -- froce index 使用
    java基础全套
    javaweb之servlet 全解
  • 原文地址:https://www.cnblogs.com/BennyKuang/p/5923847.html
Copyright © 2011-2022 走看看