c语言 实现ll 1 文法,JavaScript LL1文法 实现

JavaScriptLL1文法

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",

"":"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('正常');

}

}

每次记得刷新

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值