编译原理第一次作业

2.1

a. [ac]*a[abc]* | c*
b. ((a[bc]*a)|[bc]*)*
c. [10]*00|0
d. [01]*1[01]{6,}|(0*11[01]{4}|1011[01]{2}|101010|101011)
e. a*(b(?a)|ca*)*
f. [1-9][0-9]* | 0[1-7][0-7]*
g. 2

2.2

a. Because in the regular expression, there is no way to store that how many times a letter occurs, so we can't determine whether the count of a is larger than b.
b. equally, in the regular expression, we also can't know what have occurred before.
c. In the C programming language, there are some rules like the parenthesis, But in the regular language, we can't determine whether the parenthesis is right. Therefore, we can't use the regular language to determine whether a C program is right or not.

2.5

a.
DFA

b. Because there are too many states in this DFA, so I only write down the processing procedure and get all of the state. And I get this result by writing a Java program, and i put the Java program in the appendix. The result shows below.

  • DFAedge({1}, a) -> {1, 2}
  • DFAedge({1}, b) ->{1}
  • DFAedge({1, 2}, a) -> {1, 2, 3}
  • DFAedge({1, 2}, b) ->{1, 3}
  • DFAedge({1, 2, 3}, a) -> {1, 2, 3, 4}
  • DFAedge({1, 2, 3}, b) ->{1, 3, 4}
  • DFAedge({1, 3}, a) -> {1, 2, 4}
  • DFAedge({1, 3}, b) ->{1, 4}
  • DFAedge({1, 2, 3, 4}, a) -> {1, 2, 3, 4, 5}
  • DFAedge({1, 2, 3, 4}, b) ->{1, 3, 4, 5}
  • DFAedge({1, 3, 4}, a) -> {1, 2, 4, 5}
  • DFAedge({1, 3, 4}, b) ->{1, 4, 5}
  • DFAedge({1, 2, 4}, a) -> {1, 2, 3, 5}
  • DFAedge({1, 2, 4}, b) ->{1, 3, 5}
  • DFAedge({1, 4}, a) -> {1, 2, 5}
  • DFAedge({1, 4}, b) ->{1, 5}
  • DFAedge({1, 2, 3, 4, 5}, a) -> {1, 2, 3, 4, 5, 6}
  • DFAedge({1, 2, 3, 4, 5}, b) ->{1, 3, 4, 5, 6}
  • DFAedge({1, 3, 4, 5}, a) -> {1, 2, 4, 5, 6}
  • DFAedge({1, 3, 4, 5}, b) ->{1, 4, 5, 6}
  • DFAedge({1, 2, 4, 5}, a) -> {1, 2, 3, 5, 6}
  • DFAedge({1, 2, 4, 5}, b) ->{1, 3, 5, 6}
  • DFAedge({1, 4, 5}, a) -> {1, 2, 5, 6}
  • DFAedge({1, 4, 5}, b) ->{1, 5, 6}
  • DFAedge({1, 2, 3, 5}, a) -> {1, 2, 3, 4, 6}
  • DFAedge({1, 2, 3, 5}, b) ->{1, 3, 4, 6}
  • DFAedge({1, 3, 5}, a) -> {1, 2, 4, 6}
  • DFAedge({1, 3, 5}, b) ->{1, 4, 6}
  • DFAedge({1, 2, 5}, a) -> {1, 2, 3, 6}
  • DFAedge({1, 2, 5}, b) ->{1, 3, 6}
  • DFAedge({1, 5}, a) -> {1, 2, 6}
  • DFAedge({1, 5}, b) ->{1, 6}
  • DFAedge({1, 2, 3, 4, 5, 6}, a) -> {1, 2, 3, 4, 5, 6}
  • DFAedge({1, 2, 3, 4, 5, 6}, b) ->{1, 3, 4, 5, 6}
  • DFAedge({1, 3, 4, 5, 6}, a) -> {1, 2, 4, 5, 6}
  • DFAedge({1, 3, 4, 5, 6}, b) ->{1, 4, 5, 6}
  • DFAedge({1, 2, 4, 5, 6}, a) -> {1, 2, 3, 5, 6}
  • DFAedge({1, 2, 4, 5, 6}, b) ->{1, 3, 5, 6}
  • DFAedge({1, 4, 5, 6}, a) -> {1, 2, 5, 6}
  • DFAedge({1, 4, 5, 6}, b) ->{1, 5, 6}
  • DFAedge({1, 2, 3, 5, 6}, a) -> {1, 2, 3, 4, 6}
  • DFAedge({1, 2, 3, 5, 6}, b) ->{1, 3, 4, 6}
  • DFAedge({1, 3, 5, 6}, a) -> {1, 2, 4, 6}
  • DFAedge({1, 3, 5, 6}, b) ->{1, 4, 6}
  • DFAedge({1, 2, 5, 6}, a) -> {1, 2, 3, 6}
  • DFAedge({1, 2, 5, 6}, b) ->{1, 3, 6}
  • DFAedge({1, 5, 6}, a) -> {1, 2, 6}
  • DFAedge({1, 5, 6}, b) ->{1, 6}
  • DFAedge({1, 2, 3, 4, 6}, a) -> {1, 2, 3, 4, 5}
  • DFAedge({1, 2, 3, 4, 6}, b) ->{1, 3, 4, 5}
  • DFAedge({1, 3, 4, 6}, a) -> {1, 2, 4, 5}
  • DFAedge({1, 3, 4, 6}, b) ->{1, 4, 5}
  • DFAedge({1, 2, 4, 6}, a) -> {1, 2, 3, 5}
  • DFAedge({1, 2, 4, 6}, b) ->{1, 3, 5}
  • DFAedge({1, 4, 6}, a) -> {1, 2, 5}
  • DFAedge({1, 4, 6}, b) ->{1, 5}
  • DFAedge({1, 2, 3, 6}, a) -> {1, 2, 3, 4}
  • DFAedge({1, 2, 3, 6}, b) ->{1, 3, 4}
  • DFAedge({1, 3, 6}, a) -> {1, 2, 4}
  • DFAedge({1, 3, 6}, b) ->{1, 4}
  • DFAedge({1, 2, 6}, a) -> {1, 2, 3}
  • DFAedge({1, 2, 6}, b) ->{1, 3}
  • DFAedge({1, 6}, a) -> {1, 2}
  • DFAedge({1, 6}, b) ->{1}

2.6

  • the state 8 and the state 2 is equal.


    Step 1
  • the state 5 and the state 1 is equal


    Step 2
  • the state 4 and the state 6 is equal


    Step 3

    The last five states are all different, so, the minimum number of states is 5.

Appendix

import java.util.Vector;
public class Test {
    public static void main(String argv[]) {
        int []a = new int[200];
        //initialize the array which can determine whether a state occur or not
        for(int i = 0; i < 200; i++)a[i] = -1;
        state g1;
        Vector<state> q;
        //Use an algorithm like BFS to go through all the edge
        q = new Vector<state>();
        //BFS from the initial state
        g1 = new state();
        g1.s[1] = true;
        q.add(g1);
        while (q.size() != 0) {
            int count = 0, k = 0;
            //the new state with the input a and b
            state s1 = new state(), s2 = new state();
            state tmp = q.firstElement();
            q.remove(0);
            //determine whether this state occur before
            for(int i = 1; i < 7; i++){
                if(tmp.s[i] == true)count += i*i*i*i*i;
            }
            for(k = 0; k < 200; k++){
                if(a[k] == -1 || count == a[k])break;
            }
            if(a[k] != -1)continue;
            else a[k] = count;
            //set the next two states
            if (tmp.s[1] == true) {
                s1.s[1] = true;
                s1.s[2] = true;
                s2.s[1] = true;
            }
            for (int i = 2; i < 6; i++) {
                if (tmp.s[i] == true) {
                    s1.s[i + 1] = true;
                    s2.s[i + 1] = true;
                }
            }
            q.addElement(s1); q.addElement(s2);
            //output the result
            System.out.print("+ DFAedge({");
            for(k = 1; k < 7; k++){
                if(tmp.s[k] == true){
                    System.out.printf("%d", k);
                    break;
                }
            }
            for (int i = k + 1; i < 7; i++) {
                if (tmp.s[i] == true) System.out.printf(", %d", i);
            }
            System.out.printf("}, a) -> {");
            for(k = 1; k < 7; k++){
                if(s1.s[k] == true){
                    System.out.printf("%d", k);
                    break;
                }
            }
            for (int i = k + 1; i < 7; i++) {
                if (s1.s[i] == true) System.out.printf(", %d", i);
            }
            System.out.printf("}\n+ DFAedge({");
            for(k = 1; k < 7; k++){
                if(tmp.s[k] == true){
                    System.out.printf("%d", k);
                    break;
                }
            }
            for (int i = k + 1; i < 7; i++) {
                if (tmp.s[i] == true) System.out.printf(", %d", i);
            }
            System.out.printf("}, b) ->{");
            for(k = 1; k < 7; k++){
                if(s2.s[k] == true){
                    System.out.printf("%d", k);
                    break;
                }
            }
            for (int i = k + 1; i < 7; i++) {
                if (s2.s[i] == true) System.out.printf(", %d", i);
            }
            System.out.print("}\n");
        }
    }
}
//store one state in the DFA
class state{
    public boolean [] s = new boolean[7];
    public state(){s[0] = s[1] = s[2] = s[3] = s[4] = s[5] = s[6] = false;}
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,273评论 6 515
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,349评论 3 398
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,709评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,520评论 1 296
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,515评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,158评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,755评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,660评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,203评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,287评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,427评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,122评论 5 349
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,801评论 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,272评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,393评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,808评论 3 376
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,440评论 2 359

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,554评论 0 23
  • 我一定得坚强一点,要不然什么都完了。 杨诚实在输液的时候睡了一觉。醒来后感觉好多了。他对着窗外心里在喊,你不要老跟...
    赵伟亭阅读 370评论 0 0
  • 森林上空出现了一片鱼群,随时间变换为不同的形状,鳞片反射着阳光,就像一大片散射着太阳光辉的云彩。 我在鱼群之下,不...
    时间轮回中的七月之雨阅读 326评论 0 6
  • 快丧失了对人诉说的需要 坏的东西都在靠自己慢慢消化 一点点痊愈 我还可以 反正捂着枕头哭也不会死
    825a95066721阅读 142评论 0 0
  • 对于跟大家一块作这件事,我并没有想的那么多,只是一直觉得自己语言组织能力,和表达能力很差,想借此机会锻炼一下。这就...
    一个意外的结尾阅读 148评论 0 0