---
title: "Javascript文法之var声明 2"
author: "phyeas"
date: "2009-09-20T16:16:51Z"
tags: []
slug: "javascript-var-2"
draft: false
lastmod: "2009-09-20T16:16:51Z"
id: 472394
url: "https://phyeas.iteye.com/blog/472394"
---

经过 [rednaxelafx](http://rednaxelafx.iteye.com/) 的指点和本人的努力，新一版版的文法终于出来了，呵呵![](/images/smiles/icon_lol.gif)

这次终于让文法可以识别function。并且将上次的有递归语法改为左递归([rednaxelafx](http://rednaxelafx.iteye.com/) 的功劳)

又自己重写了一遍，熟悉一下思路。文法这东西确实不好写。

以下是文法文件的内容：

```
%{
#include "stdio.h"
%}
/*KeyWord*/
%token TYPEOF VAR FUNCTION RETURN FOR WHILE DO IF ELSE BREAK DELETE TRY CATCH IN SWITCH FINALLY NEW CASE CONTINUE THROW
/*value*/
%token __NULL__ THIS TRUE FALSE ID NUM STRING REGEX
/*relation operator*/
%token EQEQ EQEQEQ GT GE LT LE NE NEE
/*calculation operator*/
%token PLUS SUB MUL MOD DIV PLUS_EQ SUB_EQ MUL_EQ DIV_EQ MOD_EQ DPLUS DSUB
/*logiclOperator*/
%token AND OR NOT
/*other*/
%token SEMICOLON EQ POINT LPAREN RPAREN LBRACK RBRACK LBRACE RBRACE COMMA COLON
%left OR
%left AND
%right NOT
%left LT GT EQEQ LE GE NE EQEQEQ NEE
%left PLUS SUB
%left DPLUS DSUB
%right PREDPLUS
%left MUL DIV
%left POINT
%right NEGFIRST
%%
statementList:
	variableDeclareExpression
	| statementList SEMICOLON variableDeclareExpression
	|
;
variableDeclareExpression:
	VAR variableIdDeclareExpression  {printf("var declare\n");}
;
variableIdDeclareExpression:
	ID variableDeclareValueExpression
	| variableIdDeclareExpression COMMA ID variableDeclareValueExpression
;
variableDeclareValueExpression:
	EQ expression | /*nothing*/
;
expression:
	relationalExpression
	| expression OR relationalExpression
	| expression AND relationalExpression
;
relationalExpression:
	calculationExpression 
	| calculationExpression LT calculationExpression
	| calculationExpression GT calculationExpression
	| calculationExpression EQEQ calculationExpression
	| calculationExpression LE calculationExpression
	| calculationExpression GE calculationExpression
	| calculationExpression NE calculationExpression
	| calculationExpression EQEQEQ calculationExpression
	| calculationExpression NEE calculationExpression
;
calculationExpression:
	primaryExpression
	| calculationExpression PLUS primaryExpression
	| calculationExpression SUB primaryExpression
	| calculationExpression MUL primaryExpression
	| calculationExpression DIV primaryExpression
	| calculationExpression MOD primaryExpression
;
primaryExpression:
	primaryExpressionFactor
	| primaryExpressionFactor LBRACK expression RBRACK
	| primaryExpressionFactor LPAREN functionParametersExpression RPAREN
	| primaryExpressionFactor DPLUS
	| primaryExpressionFactor DSUB
;
primaryExpressionFactor:
	__NULL__
	| TRUE
	| FALSE
	| NUM
	| LPAREN expression RPAREN
	| DPLUS primaryExpressionFactor %prec PREDPLUS
	| DSUB primaryExpressionFactor %prec PREDPLUS
	| SUB primaryExpressionFactor %prec NEGFIRST
	| nonameFunctionDeclare
	| STRING
	| ID 
	| REGEX
	| primaryExpressionFactor POINT ID
	| newObjectDeclareExpression
	| newArrayDeclareExpression
;
functionParametersExpression:
	expression
	| functionParametersExpression COMMA expression
	|
;
nonameFunctionDeclare:
	FUNCTION LPAREN argumentList RPAREN LBRACE statementList RBRACE
;
newObjectDeclareExpression:
	NEW ID LPAREN functionParametersExpression RPAREN
;
newArrayDeclareExpression:
	LBRACK functionParametersExpression RBRACK
;
argumentList:
	ID
	| argumentList COMMA ID
	|
;
%%
```