Home>Article>Web Front-end> Use javascript to write lexical analysis of four arithmetic compilers
There is a course called Compilation Principles in junior year, which asks us to write a simple compiler ourselves. Well, any language can be used. Of course I use js. It is so elegant, although I don’t use it very much. grace. This has nothing to do with language, it’s just that I like to use js, and there aren’t many js features used in it.
Also, the code is a bit bad, so don’t complain.
Let me talk about my whole process first
The first step is lexical analysis: you need to write a regular expression and then put the words and numbers in it Cut them all out.
Build grammar rules. Here I chose LL(1) grammar. Design your own grammar here.
Build intermediate code. Here I used a syntax tree.
Write a conversion program, and what kind of grammatical sentence corresponds to what kind of program.
## Lexical analysis:
"anything"One (this can be used to handle errors. If it is not the above 1-4, then you can use 5 to receive and exclude)
var id_ = new build_rule(); id_.build_from_str(id__str, 3); //这个变量id__str就是那个已经生成字符串保存起来的dfa最小化的表 //数字3就是id对应的名字,到时候用来判断来生成类型码的 var key_word = new build_rule(); key_word.build_from_str(key_word_str, 1); //和上面一样 var ops = new build_rule("{op}{op}*", 1); //这个使用正则生成的规则的,需要经过nfa---dfa---最小化这几步的转化 //1符号和关键字统称的类型 var num = new build_rule("{float}", 4); //同上 var anything = new build_rule(); anything.build_from_str(anything_str); anything.rule_name = 5; //这个就是用来处理错误的,识别5这个类型时候就会出错,也可以记录这个出错让程序一直扫描到后面再输出错误 //按照自己定义的规定的顺序进行添加规则,到时候就会按照这个顺序进行查找 var qing = qingai(code); qing.add_rules(key_word); qing.add_rules(id_); qing.add_rules(ops); qing.add_rules(num); qing.add_rules(anything); qing.action();
Variable name——–>Keyword————>Others
In this case, if
"var"is recognized, var will be regarded as a variable name, because when var is not defined as a keyword, this can be used as a legal variable name.So the order needs to be arranged by yourself
is, ensuring that everyone can find the attribute.
a=7464;b=7465;a=b+7464*2;
8. The following type code is the type code that needs to be used to express this type of string in the grammar. Later, the grammar side will use the type code to determine whether the entered sentence symbol does not conform to the given grammar. Because different keywords and symbols have different meanings, the type codes of keywords and symbols are different. My variable name is represented by d
Summary steps
Algorithms and examples of parsing four arithmetic expressions in javascript
Analysis of JavaScript pre-compilation principle
The above is the detailed content of Use javascript to write lexical analysis of four arithmetic compilers. For more information, please follow other related articles on the PHP Chinese website!