<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>JavaScriptLL1文法</title>
</head>
<script>
biaozhi = [];
shuzi = [];
zifuchangliang = [];
_stage = null;//算数表达式1,bool语句2,
STATIC = {
"and":"1","array":"2","begin":"3","bool":"4",
"call":"5","case":"6","char":"7","constant":"8",
"dim":"9","do":"10","else":"11","end":"12",
"false":"13","for":"14","if":"15","input":"16",
"integer":"17","not":"18","of":"19","or":"20","output":"21",
"procedure":"22","program":"23","read":"24","real":"25","repeat":"26",
"set":"27","stop":"28","then":"29","to":"30","true":"31","until":"32",
"var":"33","while":"34","write":"35","(":"39",")":"40","*":"41","+":"43",
",":"44","-":"45",".":"46","..":"47","/":"48",":":"50",":=":"51",";":"52",
"<":"53","<=":"54","<>":"55","=":"56",">":"57",">=":"58","[":"59","]":"60"
};
_MEMORAY = [];
_SYMBLE = [];
_state = 0;
MEMORAY = [];
_LL1State = 0;
wrong = 0;
stop = null;
var State = function () {
var state = {
state0: function () {
if (_MEMORAY.length > 0) {
var string = '';
_MEMORAY.forEach(function (item) {
string = string + item
});
MEMORAY.push(string);
}
if (_SYMBLE.length > 0) {
var symble = '';
_SYMBLE.forEach(function (item) {
symble = symble + item
});
MEMORAY.push(symble);
}
_state = 0;
_SYMBLE = [];
_MEMORAY = [];
},
state1: function (value) {
if (_state === 0 || _state === 1) {
_state = 1;
_MEMORAY.push(value)
} else {
console.log('出错');
return new Error('出错')
}
},
state3: function (value) {
if (_state === 0) {
_state = 3;
MEMORAY.push(value)
} else {
console.log('出错');
return new Error('出错')
}
},
state5: function (value) {
if(_state === 0){
_state = 5;
_SYMBLE.push(value)
}else{
console.log('出错');
return new Error('出错')
}
},
state6: function () {
},
state7: function (value) {
if(_state === 0){
_state = 7;
_SYMBLE.push(value)
}else{
console.log('出错');
return new Error('出错')
}
},
state8: function (value) {
if(_state === 0){
_state = 8;
_SYMBLE.push(value)
}else{
console.log('出错');
return new Error('出错')
}
},
state9: function (value) {
if(_state === 0){
_state = 9;
_SYMBLE.push(value)
}else{
console.log('出错');
return new Error('出错')
}
},
state10: function (value) {
if(_state === 0){
_state = 10;
_SYMBLE.push(value)
}else{
console.log('出错');
return new Error('出错')
}
},
state11: function (value) {
if(_state === 0){
_state = 11;
_SYMBLE.push(value)
}else{
console.log('出错');
return new Error('出错')
}
},
state14: function () {
if(_state === 5){
_state = 14;
_SYMBLE =[]
}else{
console.log('出错');
return new Error('出错')
}
},
state15: function (value) {
if(_state === 7){
_state = 15;
_SYMBLE.push(value)
}else{
console.log('出错');
return new Error('出错')
}
},
state16: function (value) {
if(_state === 7){
_state = 16;
_SYMBLE.push(value)
}else{
console.log('出错');
return new Error('出错')
}
},
state17: function (value) {
if(_state === 8){
_state = 17;
_SYMBLE.push(value)
}else{
console.log('出错');
return new Error('出错')
}
},
state18: function (value) {
if(_state === 9){
_state = 18;
_SYMBLE.push(value)
}else{
console.log('出错');
return new Error('出错')
}
},
state19: function (value) {
if(_state === 11){
_state = 19;
_SYMBLE.push(value)
}else{
console.log('出错');
return new Error('出错')
}
},
state20: function () {
if(_state === 14){
_state = 20;
}else{
console.log('出错');
return new Error('出错')
}
},
state21: function () {
if(_state === 20){
_state = 21;
}else{
console.log('出错');
return new Error('出错')
}
},
state22: function () {
if(_state === 10){
_state = 22;
}else{
console.log('出错');
return new Error('出错')
}
},
state500: function () {
console.log('出错');
return new Error('出错')
},
};
function showState(value) {
var num = 0;
if(_state === 3||_state === 4||_state === 6 || _state === 19
||_state === 22 ||_state === 18 || _state === 17
||_state === 16||_state === 15||_state === 21){
_state = 0;
num = 0;
state['state' + num] && state['state' + num](value);
return showState(value)
}
if(_state === 10 && value.charCodeAt(0) !== 46){
_state = 0;
num = 0;
state['state' + num] && state['state' + num](value);
return showState(value)
}
if(_state === 9 && value.charCodeAt(0) !== 61){
_state = 0;
num = 0;
state['state' + num] && state['state' + num](value);
return showState(value)
}
if(_state === 8 && value.charCodeAt(0) !== 61){
_state = 0;
num = 0;
state['state' + num] && state['state' + num](value);
return showState(value)
}
if(_state === 7 && value.charCodeAt(0) !== 61){
_state = 0;
num = 0;
state['state' + num] && state['state' + num](value);
return showState(value)
}
if(_state === 7 && value.charCodeAt(0) !== 62){
_state = 0;
num = 0;
state['state' + num] && state['state' + num](value);
return showState(value)
}
if(_state === 5 && value.charCodeAt(0) !== 42){
_state = 0;
num = 0;
state['state' + num] && state['state' + num](value);
return showState(value)
}
if (_state === 14 && value.charCodeAt(0) !== 42) {return ''}
if (_state === 20 && value.charCodeAt(0) !== 47) {
_state = 14;
return ''
}
if(_state === 1 && (new RegExp(/^[a-zA-Z]+$/)).test(value)=== false&&(new RegExp("^[0-9]*$")).test(value)=== false){
_state = 0;
num = 0;
state['state' + num] && state['state' + num](value);
return showState(value)
}
if (_state === 11 && value.charCodeAt(0) !== 39) {
_SYMBLE.push(value);
return ''
}
//空格回车初始化
if ((new RegExp(/\s+/g)).test(value) || (new RegExp(/[\r\n]/g)).test(value)) {
num = 0;
}
//字母,数字,跳到1
else if ((new RegExp(/^[a-zA-Z]+$/)).test(value) ||(new RegExp("^[0-9]*$")).test(value)) {
num = 1;
}
//一些普通单界符,跳到3
else if (
value.charCodeAt(0) === 43 ||
value.charCodeAt(0) === 45 ||
value.charCodeAt(0) === 40 ||
value.charCodeAt(0) === 41 ||
value.charCodeAt(0) === 91 ||
value.charCodeAt(0) === 93 ||
value.charCodeAt(0) === 59 ||
value.charCodeAt(0) === 44) {num = 3;}
// *符号,如果是0,跳3,如果是5,跳14
else if (
value.charCodeAt(0) === 42) {
if(_state === 0){
num = 3;
}else if(_state === 5){
num = 14;
}else if(_state === 14){
num = 20
}
}
// /符号,如果是0,跳5,如果是20,跳21
else if (
value.charCodeAt(0) === 47) {
if(_state === 0){
num = 5;
}else if(_state === 20){
num = 21;
}
}
// =符号,如果是0,跳3,如果是7,跳15,如果是8,跳17,如果是9,跳18
else if (
value.charCodeAt(0) === 61) {
if(_state === 0){
num = 3;
}else if(_state === 7){
num = 15;
}else if(_state === 8){
num = 17;
}else if(_state === 9){
num = 18;
}
}
// < 符号,如果是0,跳7
else if (value.charCodeAt(0) === 60) {num = 7;}
// > 符号,如果是0,跳8,如果是7,跳16
else if (value.charCodeAt(0) === 62) {
if(_state === 0){
num = 8;
}else if(_state === 7){
num = 16;
}
}
// : 符号,如果是0,跳9
else if (value.charCodeAt(0) === 58) {num = 9;}
// . 符号,如果是0,跳10,如果是10,跳22
else if (value.charCodeAt(0) === 46) {
if(_state === 0){
num = 10;
}else if(_state === 10){
num = 22;
}
}
// ' 符号,如果是0,跳11,如果是11,跳19
else if (value.charCodeAt(0) === 39) {
if(_state === 0){
num = 11;
}else if(_state === 11){
num = 19;
}
}else{
num = 500
}
state['state' + num] && state['state' + num](value)
}
return {
show: showState
}
}();
var LL1State = function () {
var state = {
//program识别成功
state0:function () {
_LL1State = 0;
},
//标识符,数字状态
state1:function (value) {
if(_LL1State === 2||_LL1State === 13||_LL1State === 12||_LL1State === 11||_LL1State === 0||_LL1State === 6||_LL1State === 3||_LL1State === 5||_LL1State === 7||_LL1State === 8||_LL1State === 9||_LL1State === 10){
_LL1State = 1;
}else{
wrong++;
console.log(value+'错误,不能加入')
}
},
//;识别成功
state2:function (value) {
if(_LL1State === 1||_LL1State === 5){
_LL1State = 2;
_stage = null;
stop = null
}else{
wrong++;
console.log(value+'错误,不能加入')
}
},
//var识别
state3:function (value) {
if(_LL1State === 2){
_LL1State = 3;
}else{
wrong++;
console.log(value+'错误,不能加入')
}
},
//:识别
state4:function (value) {
if(_LL1State === 1){
_LL1State = 4;
}else{
wrong++;
console.log(value+'错误,不能加入')
}
},
//bool int识别
state5:function (value) {
if(_LL1State === 4){
_LL1State = 5;
}else{
wrong++;
console.log(value+'错误,不能加入')
}
},
//begin识别
state6:function (value) {
if(_LL1State === 2){
_LL1State = 6;
stop = null
}else{
wrong++;
console.log(value+'错误,不能加入;')
}
},
//:=识别
state7:function (value) {
if(_LL1State === 1 &&_stage !== 1){
_stage = 1;
_LL1State = 7;
}else{
wrong++;
console.log(value+'错误,不能加入')
}
},
//+ - 识别
state8:function (value) {
if(_LL1State === 1 &&_stage === 1&&stop === null){
_LL1State = 8;
stop = 1;
}else{
wrong++;
console.log(value+'错误,不能加入')
}
},
//while识别
state9:function (value) {
if((_LL1State === 2||_LL1State === 11||_LL1State === 6|| _LL1State === 12)&& _stage === null){
_LL1State = 9;
_stage = 2;
stop = null
}else{
wrong++;
console.log(value+'错误,不能加入')
}
},
//<识别
state10:function (value) {
if(_LL1State === 1 && _stage === 2&&stop === null ){
_LL1State = 10;
stop = 1;
}else{
wrong++;
console.log(value+'错误,不能加入')
}
},
//do识别
state11:function (value) {
if(_LL1State === 1 && _stage === 2 ){
_LL1State = 11;
_stage = null;
stop = null
}else{
wrong++;
console.log(value+'错误,不能加入')
}
},
//repeat识别
state12:function (value) {
if((_LL1State === 2||_LL1State === 11||_LL1State === 6|| _LL1State === 12)&& _stage === null){
_LL1State = 12;
_stage = null;
stop = null
}else{
wrong++;
console.log(value+'错误,不能加入')
}
},
//until识别
state13:function (value) {
if(_LL1State === 1){
_LL1State = 13;
_stage = 2;
stop = null
}else{
wrong++;
console.log(value+'错误,不能加入')
}
},
//end识别
state14:function (value) {
if(_LL1State === 2||_LL1State === 1){
_LL1State = 14;
}else{
wrong++;
console.log(value+'错误,不能加入')
}
},
//.识别
state15:function (value) {
if(_LL1State === 14){
_LL1State = 15;
}else{
wrong++;
console.log(value+'错误,不能加入')
}
},
};
function showState(value) {
var num = 0;
if(value === 'program'){
num = 0;
}else if(biaozhi.some(function (item) {return item === value}) && _stage === null){
num = 1;
}else if((biaozhi.some(function (item) {return item === value})||shuzi.some(function (item) {return item === value})) && _stage !== null){
num = 1;
}else if(value === ';'){
num = 2;
}else if(value === 'var'){
num = 3;
}else if(value === ':'){
num = 4;
}else if(value === 'bool'||value === 'integer'){
num = 5;
}else if(value === 'begin'){
num = 6;
}else if(value === ':='){
num = 7;
}else if(value === '+'||value === '-'){
num = 8;
}else if(value === 'while'){
num = 9;
}else if(value === '<'){
num = 10;
}else if(value === 'do'){
num = 11;
}else if(value === 'repeat'){
num = 12;
}else if(value === 'until'){
num = 13;
}else if(value === 'end'){
num = 14;
}else if(value === '.'){
num = 15;
}
state['state'+num]&&state['state'+num](value)
}
return {
show:showState
}
}();
function clickMe() {
var a = document.getElementById('test').value + ' ';
for (var i = 0; i < a.length; i++) {
State.show(a.charAt(i));
}
// console.log('最终状态为'+_state);
// console.log(MEMORAY);
var json = {};
var number = 1;
MEMORAY.forEach(function (item) {
if(STATIC[item]){
// console.log('序号'+index+'('+STATIC[item]+',-)')
}else if(isNaN(Number(item)) === false){
if(!json[item]){
json[item] = number++;
shuzi.push(item)
}
// console.log('序号'+index+'(37,'+ json[item] +')')
}else if(!item.indexOf('\'')){
if(!json[item]){
json[item] = number++;
zifuchangliang.push(item)
}
// console.log('序号'+index+'(38,'+ json[item] +')')
}else{
if(!json[item]){
json[item] = number++;
biaozhi.push(item)
}
// console.log('序号'+index+'(36,'+ json[item] +')')
}
});
MEMORAY.splice(0,MEMORAY.indexOf('program'));
// console.log(MEMORAY);
MEMORAY.forEach(function (item) {
LL1State.show(item)
});
if(wrong>0){
console.log('LL1最终状态为'+_LL1State+',不正常');
}else{
console.log('正常');
}
}
</script>
<body>
<div id="container">
<label>
<textarea rows="30" cols="50" id="test"></textarea><br><br>
<button onclick="clickMe()">每次记得刷新</button>
</label>
</div>
</body>
</html>
JavaScript LL1文法 实现
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 一个完整的JavaScript实验应该由下列三个不同的部分组成。 核心 (ECMAScript)文档对象模型 (D...
- Python / Ruby 等脚本语言具备了一种采用命名参数的来进行函数调用的方式。这对于参数数目很多并且大部分都...
- Javascript实现图片的预加载的完整实现 2012-11-01 11:51 by Zhuang miao,2...
- 这是一个经典的算法题,首先有两个有序数组a = [1, 2, 3, 6, 7, 9],b = [2, 4, 6, ...