Code Monkey home page Code Monkey logo

compiler-principle's Introduction

Introduction

编译原理实验

  • 词法分析器和LL(1)文法核心代码均采用C++实现,服务端代码使用Koa2实现,前端可视化代码使用React实现 js作为胶水层(node-ffi)将c++运行的结果转发给前端,数据格式使用json
  • LR(1)文法我用的是JavaScript,原因是我做LL(1)文法的时候,要把预测分析表可视化出来,这样去拼接json字符串给nodejs太麻烦了 于是我就用了JavaScript,便于可视化图表等,而且数据结构也比C++好用

使用

npm install 安装依赖
npm run server 开启服务端
npm run dev 开启react开发
npm start 同时启动server和dev

环境:

windows10 + vs2015 + nodejs-v8.9.3

  • ffi引用dll库时参考的文章
  • ffi引入函数名错误参考的文章
  • 编译dll的时候,在CompilersPrinciplesDll.cpp文件里面引入iostream, string等头文件报错,将头文件引入至stdafx.h即可解决
  • ffi里面的string类型对应c++是char* 类型的,而我需要使用string,直接使用string无法获取参数和结果,所以写了个函数转换
  • 其实可以不用ffi的,但是要安装c++编译器,利用child_process.exec去执行cpp文件,然后利用exec返回的子进程进行管道通信
// char* 可直接赋值给string
// string 转 char*,用来返回结果给nodejs
char* to_char_pointer(string str) {
	int length = (int)str.length();
	char *p = new char[length + 1];
	for (int i = 0; i < length; ++i) {
		p[i] = str[i];
	}
	p[length] = '\0'; //加上结束符
	return p;
}

效果预览

  • 词法分析

词法分析我是利用循环来实现自动机匹配标识符,数字等等 词法分析效果图

  • LL(1)

LL的关键在于求出first集和follow集,求first集的时候要注意消除左递归,然后根据first集和follow集求出预测分析表 LL(1)效果图

  • LR(1)

LR的关键在于goto函数和closure函数,只要把这两个函数写出来,整个实验就完成了大半了,当然,这里面的项目还有超前搜索符,这个需要考虑怎么表示,个人觉得使用脚本语言或者c++都挺好写的,c++有结构体,python有字典,JavaScript有字面量对象,但是Java纯面向对象的语言表示这个感觉有点鸡肋。 LR(1)效果图

compiler-principle's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

1914555303

compiler-principle's Issues

构建错误

您好,我尝试运行您的代码,npm install成功无错误,但npm start server输出如下:

> [email protected] start /home/newcoderlife/Compiler-Principle
> node index.js "server"

internal/modules/cjs/loader.js:605
    throw err;
    ^

Error: Cannot find module 'koa'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:603:15)
    at Function.Module._load (internal/modules/cjs/loader.js:529:25)
    at Module.require (internal/modules/cjs/loader.js:657:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (/home/newcoderlife/Compiler-Principle/http/app.js:1:75)
    at Module._compile (internal/modules/cjs/loader.js:721:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
    at Function.Module._load (internal/modules/cjs/loader.js:552:3)
internal/modules/cjs/loader.js:605
    throw err;
    ^

Error: Cannot find module 'koa'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:603:15)
    at Function.Module._load (internal/modules/cjs/loader.js:529:25)
    at Module.require (internal/modules/cjs/loader.js:657:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (/home/newcoderlife/Compiler-Principle/http/app.js:1:75)
    at Module._compile (internal/modules/cjs/loader.js:721:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
    at Function.Module._load (internal/modules/cjs/loader.js:552:3)
{ Error: Command failed: cd view && npm start
sh: 1: webpack-dev-server: not found
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! [email protected] start: `webpack-dev-server --hot --inline --color --hot --config ./scripts/webpack.dev.js`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/newcoderlife/.npm/_logs/2018-12-20T07_11_02_831Z-debug.log

    at ChildProcess.exithandler (child_process.js:294:12)
    at ChildProcess.emit (events.js:189:13)
    at maybeClose (internal/child_process.js:978:16)
    at Socket.stream.socket.on (internal/child_process.js:395:11)
    at Socket.emit (events.js:189:13)
    at Pipe._handle.close (net.js:610:12)
  killed: false,
  code: 1,
  signal: null,
  cmd: 'cd view && npm start' }
{ Error: Command failed: cd view && npm start
sh: 1: webpack-dev-server: not found
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! [email protected] start: `webpack-dev-server --hot --inline --color --hot --config ./scripts/webpack.dev.js`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/newcoderlife/.npm/_logs/2018-12-20T07_11_04_096Z-debug.log

    at ChildProcess.exithandler (child_process.js:294:12)
    at ChildProcess.emit (events.js:189:13)
    at maybeClose (internal/child_process.js:978:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:265:5)
  killed: false,
  code: 1,
  signal: null,
  cmd: 'cd view && npm start' }

根据错误信息,我运行了sudo npm install koa -g,仍旧报错。

希望能够得到您的帮助,谢谢!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.