Грамматика C + Код

Материал из Вики проекта PascalABC.NET
Версия от 22:13, 28 сентября 2011; Extremall (обсуждение | вклад) (Новая: <source lang=""> progr: { $$ = new Program(@$); } | progr OperatorSequence { for (int i = 0; i < $2.list.Count) $1.list.Add($2.list[i]); $$ = $1; } ; Operator: def { $$ ...)
(разн.) ← Предыдущая версия | Текущая версия (разн.) | Следующая версия → (разн.)
Перейти к навигацииПерейти к поиску
progr: {
		$$ = new Program(@$);
	}
	| progr OperatorSequence {
		for (int i = 0; i < $2.list.Count)
			$1.list.Add($2.list[i]);
		$$ = $1;
	}
	;
 
Operator: def {
		$$ = $1;
	}
	| assign {
		$$ = $1;
	}
	| if {
		$$ = $1;
	}
	| while {
		$$ = $1;
	}
	| OutPut
	| BOperator {
		$$ = $1;
	}
	;
 
BOperator: BEGIN OperatorSequense END {
		$$ = $2;
	}
	;
 
OperatorSequense: {
		$$ = new BlockOperator(@$);
	}
	| Operatorsequense operator {
		$1.list.Add($2);
		$$ = $1;		
	}
	;
 
def: ident varlist SEMICOLUMN{
		TipType type;
		if ($1 == "int")
			type = TipType.IntType;
		else if ($1 == "double")
			type = TipType.DoubleType;
		else if ($1 == "bool")
			type = TipType.BoolType;
		else
		{
			// Error: unknown type
		}
		$$ = new VarDef(@$, $2, type);
	}
	;
 
varlist: ident {
		if (GlobalStructures.isNameExists($1))
		{
			// Error: this name is already used
		}
		Ident Id = new Ident($1, @1);
		listVars.Add(Id);
		$$ = new List<Ident>();
		$$.Add(Id);
	}
	| varlist COMMA ident{
		if (GlobalStructures.isNameExists($3))
		{
			// Error: this name is already used
		}
		Ident Id = new Ident($3, @3);
		listVars.Add(Id);
		$1.Add(Id);
		$$ = $1;
	}
	;
 
assgin: ident ASS expr SEMICOLUMN {
		if (!GlobalStructures.isNameExists($1))
		{
			// Error: Undeclarated identifier
		}
		Ident Id = GlobalStructures.getIdentByName($1);
		$$ = new Assign(Id, $3, @3);
	}
	;
 
expr 	
	: ident {
		if (!GlobalStructures.isNameExists($1))
		{
			// Error: Undeclarated identifier
		}
		$$ = new Ident($1, @1);
	}
	| INTNUM {
		int i = int.Parse($1);
		$$ = new IntConst(i, @1);
	}
	| REALNUM {
		double d = double.Parse($1);
		$$ = new DoubleConst(d, @1);
	}
	| TRUE {
		$$ = new BoolConst(true, @$);
	}
	| FALSE{
		$$ = new BoolConst(false, @$);
	}
	| MINUS expr %prec UMINUS {
		if ($2.getType() != TipType.IntType && $2.getType() != TipType.DoubleType)
		{
			// Error: type is wrong.
		}
		$$ = new UnarExpression($2, Op.Minus, @2);
	}
	| LB expr RB {
		$$ = $2;
	}
	| expr PLUS expr {
		$$ = new BinExpression($1, $3, Op.Plus, @$);
	}
	| expr MINUS expr {
		$$ = new BinExpression($1, $3, Op.Minus, @$);
	}
	| expr MULT expr {
		$$ = new BinExpression($1, $3, Op.Mult, @$);
	}
	| expr DIVIDE expr {
		$$ = new BinExpression($1, $3, Op.Divide, @$);
	}
	| expr EQ expr {
		$$ = new BinExpression($1, $3, Op.Equal, @$);
	}
	| expr NE expr {
		$$ = new BinExpression($1, $3, Op.NotEqual, @$);
	}
	| expr LT expr {
		$$ = new BinExpression($1, $3, Op.Less, @$);
	}
	| expr LE expr {
		$$ = new BinExpression($1, $3, Op.LessEqual, @$);
	}
	| expr GT expr {
		$$ = new BinExpression($1, $3, Op.More, @$);
	}
	| expr GE expr {
		$$ = new BinExpression($1, $3, Op.MoreEqual, @$);
	}
	;
  
if: IF LB expr RB operator elsepart {
		if ($3.getType()!=TypType.Boolean)
		{
			// Error: Условие должно иметь логический тип
		}
		$$=new If($3,$5,$6,@$);
	}
	;
 
elsepart: { $$ = null; }
	| operator {
		$$ = $1;
	}
 
while: WHILE LB expr RB operator {
		if ($3.getType()!=TypType.Boolean)
		{
			// Error: Условие должно иметь логический тип
		}
		$$ = new While($3, $5, @$);
	}