// This code was generated by the Gardens Point Parser Generator
// Copyright (c) Wayne Kelly, QUT 2005-2007
// (see accompanying GPPGcopyright.rtf)

// GPPG version 1.2.0.115 (2007-11-7)
// options: no-lines conflicts


using System;
using System.Collections.Generic;
using System.Text;
using gppg;
using analex;
using SymbolTable;

public enum Tokens
{
    error = 1, EOF = 2, qRel = 3, qIdentifier = 4, qBool = 5, qInt = 6, qReal = 7, qString = 8,
    qVoid = 9, qChar = 10, qSet = 11, qLogical = 12, qSemicolon = 13, qValInt = 14, qValString = 15, qXml = 16,
    qKeyCode = 17, qKeyParam = 18, qKeyAction = 19, qComment = 20, qMul = 21, qOr = 22, qLeft1 = 23, qRight1 = 24,
    qNot = 25, qValReal = 26, qAdd = 27, qKeyVar = 28, qKeyConst = 29, qKeyMain = 30, qKeyFunction = 31, qComma = 32,
    qRet = 33, qFor = 34, qFor2 = 35, qDo = 36, qLeft4 = 37, qRight4 = 38, qDot = 39, qDot2 = 40,
    qIf = 41, qThen = 42, qElse = 43, qKeyExtern = 44, qLeft2 = 45, qLeft5 = 46, qRight2 = 47, qRight5 = 48,
    qERROR = 49
};

public class Parser : ShiftReduceParser<SemInfo, LexLocation>
{

    static CRecordInfo recInfo;
    static string temp;
    static int IntTemp;
    static bool BoolTemp;
    static float RealTemp;

    static DecLvl declLvl = DecLvl.DL_Main;

    static CFuncInfo actFunc = null;

    protected override void Initialize()
    {
        this.errToken = (int)Tokens.error;
        this.eofToken = (int)Tokens.EOF;

        states = new State[163];
        AddState(0, new State(new int[] { 3, 68, 1, 162, 29, -9, 28, -9 }, new int[] { -1, 1, -3, 3, -24, 75, -25, 5, -30, 55, -4, 76, -5, 77 }));
        AddState(1, new State(new int[] { 2, 2 }));
        AddState(2, new State(-1));
        AddState(3, new State(new int[] { 3, 68, 2, -2 }, new int[] { -24, 4, -25, 5, -30, 55 }));
        AddState(4, new State(-38));
        AddState(5, new State(new int[] { 4, 21, 16, 52 }, new int[] { -26, 6 }));
        AddState(6, new State(new int[] { 3, 18 }, new int[] { -27, 7 }));
        AddState(7, new State(new int[] { 15, 8 }));
        AddState(8, new State(new int[] { 13, 9 }));
        AddState(9, new State(new int[] { 16, 15 }, new int[] { -28, 10 }));
        AddState(10, new State(new int[] { 16, 12 }, new int[] { -29, 11 }));
        AddState(11, new State(-39));
        AddState(12, new State(new int[] { 19, 13 }));
        AddState(13, new State(new int[] { 3, 14 }));
        AddState(14, new State(-50));
        AddState(15, new State(new int[] { 17, 16 }));
        AddState(16, new State(new int[] { 3, 17 }));
        AddState(17, new State(-49));
        AddState(18, new State(new int[] { 17, 19 }));
        AddState(19, new State(new int[] { 3, 20 }));
        AddState(20, new State(-48));
        AddState(21, new State(new int[] { 7, 46, 5, 47, 6, 48, 8, 49, 10, 50, 9, 51 }, new int[] { -9, 22 }));
        AddState(22, new State(new int[] { 13, 23, 11, 24 }));
        AddState(23, new State(-43));
        AddState(24, new State(new int[] { 14, 32, 26, 33, 15, 34, 12, 35, 4, 36, 23, 37, 25, 43 }, new int[] { -10, 25, -32, 45, -33, 42, -34, 31 }));
        AddState(25, new State(new int[] { 13, 26, 27, 27, 22, 40 }));
        AddState(26, new State(-44));
        AddState(27, new State(new int[] { 14, 32, 26, 33, 15, 34, 12, 35, 4, 36, 23, 37, 25, 43 }, new int[] { -32, 28, -33, 42, -34, 31 }));
        AddState(28, new State(new int[] { 21, 29, 13, -57, 27, -57, 22, -57, 24, -57, 35, -57, 36, -57, 32, -57 }));
        AddState(29, new State(new int[] { 14, 32, 26, 33, 15, 34, 12, 35, 4, 36, 23, 37, 25, 43 }, new int[] { -33, 30, -34, 31 }));
        AddState(30, new State(-60));
        AddState(31, new State(-62));
        AddState(32, new State(-65));
        AddState(33, new State(-66));
        AddState(34, new State(-67));
        AddState(35, new State(-68));
        AddState(36, new State(-69));
        AddState(37, new State(new int[] { 14, 32, 26, 33, 15, 34, 12, 35, 4, 36, 23, 37, 25, 43 }, new int[] { -10, 38, -32, 45, -33, 42, -34, 31 }));
        AddState(38, new State(new int[] { 24, 39, 27, 27, 22, 40 }));
        AddState(39, new State(-63));
        AddState(40, new State(new int[] { 14, 32, 26, 33, 15, 34, 12, 35, 4, 36, 23, 37, 25, 43 }, new int[] { -32, 41, -33, 42, -34, 31 }));
        AddState(41, new State(new int[] { 21, 29, 13, -58, 27, -58, 22, -58, 24, -58, 35, -58, 36, -58, 32, -58 }));
        AddState(42, new State(-61));
        AddState(43, new State(new int[] { 14, 32, 26, 33, 15, 34, 12, 35, 4, 36, 23, 37, 25, 43 }, new int[] { -33, 44, -34, 31 }));
        AddState(44, new State(-64));
        AddState(45, new State(new int[] { 21, 29, 13, -59, 27, -59, 22, -59, 24, -59, 35, -59, 36, -59, 32, -59 }));
        AddState(46, new State(-51));
        AddState(47, new State(-52));
        AddState(48, new State(-53));
        AddState(49, new State(-54));
        AddState(50, new State(-55));
        AddState(51, new State(-56));
        AddState(52, new State(new int[] { 18, 53 }));
        AddState(53, new State(new int[] { 3, 54 }));
        AddState(54, new State(-47));
        AddState(55, new State(new int[] { 4, 56, 16, 52 }, new int[] { -26, 62 }));
        AddState(56, new State(new int[] { 7, 46, 5, 47, 6, 48, 8, 49, 10, 50, 9, 51 }, new int[] { -9, 57 }));
        AddState(57, new State(new int[] { 13, 58, 11, 59 }));
        AddState(58, new State(-41));
        AddState(59, new State(new int[] { 14, 32, 26, 33, 15, 34, 12, 35, 4, 36, 23, 37, 25, 43 }, new int[] { -10, 60, -32, 45, -33, 42, -34, 31 }));
        AddState(60, new State(new int[] { 13, 61, 27, 27, 22, 40 }));
        AddState(61, new State(-42));
        AddState(62, new State(new int[] { 3, 18 }, new int[] { -27, 63 }));
        AddState(63, new State(new int[] { 15, 64 }));
        AddState(64, new State(new int[] { 13, 65 }));
        AddState(65, new State(new int[] { 16, 15 }, new int[] { -28, 66 }));
        AddState(66, new State(new int[] { 16, 12 }, new int[] { -29, 67 }));
        AddState(67, new State(-40));
        AddState(68, new State(new int[] { 4, 69 }));
        AddState(69, new State(new int[] { 3, 70 }));
        AddState(70, new State(new int[] { 3, 72 }, new int[] { -31, 71 }));
        AddState(71, new State(-45));
        AddState(72, new State(new int[] { 18, 73 }));
        AddState(73, new State(new int[] { 3, 74 }));
        AddState(74, new State(-46));
        AddState(75, new State(-37));
        AddState(76, new State(-3));
        AddState(77, new State(new int[] { 29, 129, 28, -6, 3, -6 }, new int[] { -6, 78 }));
        AddState(78, new State(new int[] { 28, 122, 3, -11 }, new int[] { -7, 79 }));
        AddState(79, new State(new int[] { 3, 135 }, new int[] { -8, 80, -11, 81, -16, 82, -12, 142, -14, 156 }));
        AddState(80, new State(-5));
        AddState(81, new State(-12));
        AddState(82, new State(-9, new int[] { -5, 83 }));
        AddState(83, new State(new int[] { 29, 129, 28, -6, 13, -6, 4, -6, 34, -6, 16, -6 }, new int[] { -6, 84 }));
        AddState(84, new State(new int[] { 28, 122, 13, 90, 4, 91, 34, 100, 16, -25 }, new int[] { -13, 85, -18, 121, -19, 95, -23, 96, -20, 106, -21, 107, -22, 113 }));
        AddState(85, new State(new int[] { 16, 86, 13, 90, 4, 91, 34, 100 }, new int[] { -18, 89, -19, 95, -23, 96, -20, 106, -21, 107, -22, 113 }));
        AddState(86, new State(new int[] { 30, 87 }));
        AddState(87, new State(new int[] { 3, 88 }));
        AddState(88, new State(-19));
        AddState(89, new State(-24));
        AddState(90, new State(-26));
        AddState(91, new State(new int[] { 11, 92, 4, -34, 13, -34 }));
        AddState(92, new State(new int[] { 14, 32, 26, 33, 15, 34, 12, 35, 4, 36, 23, 37, 25, 43 }, new int[] { -10, 93, -32, 45, -33, 42, -34, 31 }));
        AddState(93, new State(new int[] { 13, 94, 27, 27, 22, 40 }));
        AddState(94, new State(-27));
        AddState(95, new State(-28));
        AddState(96, new State(new int[] { 36, 97 }));
        AddState(97, new State(new int[] { 13, 90, 4, 91, 34, 100, 46, 118, 16, -25, 48, -25, 33, -25 }, new int[] { -17, 98, -18, 99, -19, 95, -23, 96, -20, 106, -21, 107, -22, 113 }));
        AddState(98, new State(-36));
        AddState(99, new State(-21));
        AddState(100, new State(new int[] { 4, 101 }));
        AddState(101, new State(new int[] { 11, 102 }));
        AddState(102, new State(new int[] { 14, 32, 26, 33, 15, 34, 12, 35, 4, 36, 23, 37, 25, 43 }, new int[] { -10, 103, -32, 45, -33, 42, -34, 31 }));
        AddState(103, new State(new int[] { 35, 104, 27, 27, 22, 40 }));
        AddState(104, new State(new int[] { 14, 32, 26, 33, 15, 34, 12, 35, 4, 36, 23, 37, 25, 43 }, new int[] { -10, 105, -32, 45, -33, 42, -34, 31 }));
        AddState(105, new State(new int[] { 27, 27, 22, 40, 36, -35 }));
        AddState(106, new State(-29));
        AddState(107, new State(new int[] { 13, 108, 32, 109 }));
        AddState(108, new State(-30));
        AddState(109, new State(new int[] { 4, 110 }));
        AddState(110, new State(new int[] { 11, 111 }));
        AddState(111, new State(new int[] { 14, 32, 26, 33, 15, 34, 12, 35, 4, 36, 23, 37, 25, 43 }, new int[] { -10, 112, -32, 45, -33, 42, -34, 31 }));
        AddState(112, new State(new int[] { 27, 27, 22, 40, 13, -33, 32, -33 }));
        AddState(113, new State(new int[] { 4, 114, 13, 117 }));
        AddState(114, new State(new int[] { 11, 115 }));
        AddState(115, new State(new int[] { 14, 32, 26, 33, 15, 34, 12, 35, 4, 36, 23, 37, 25, 43 }, new int[] { -10, 116, -32, 45, -33, 42, -34, 31 }));
        AddState(116, new State(new int[] { 27, 27, 22, 40, 13, -32, 32, -32 }));
        AddState(117, new State(-31));
        AddState(118, new State(new int[] { 13, 90, 4, 91, 34, 100, 48, -25 }, new int[] { -13, 119, -18, 121, -19, 95, -23, 96, -20, 106, -21, 107, -22, 113 }));
        AddState(119, new State(new int[] { 48, 120, 13, 90, 4, 91, 34, 100 }, new int[] { -18, 89, -19, 95, -23, 96, -20, 106, -21, 107, -22, 113 }));
        AddState(120, new State(-22));
        AddState(121, new State(-23));
        AddState(122, new State(new int[] { 7, 46, 5, 47, 6, 48, 8, 49, 10, 50, 9, 51 }, new int[] { -9, 123 }));
        AddState(123, new State(new int[] { 4, 124 }));
        AddState(124, new State(new int[] { 13, 125, 11, 126 }));
        AddState(125, new State(-7));
        AddState(126, new State(new int[] { 14, 32, 26, 33, 15, 34, 12, 35, 4, 36, 23, 37, 25, 43 }, new int[] { -10, 127, -32, 45, -33, 42, -34, 31 }));
        AddState(127, new State(new int[] { 13, 128, 27, 27, 22, 40 }));
        AddState(128, new State(-8));
        AddState(129, new State(new int[] { 7, 46, 5, 47, 6, 48, 8, 49, 10, 50, 9, 51 }, new int[] { -9, 130 }));
        AddState(130, new State(new int[] { 4, 131 }));
        AddState(131, new State(new int[] { 11, 132 }));
        AddState(132, new State(new int[] { 14, 32, 26, 33, 15, 34, 12, 35, 4, 36, 23, 37, 25, 43 }, new int[] { -10, 133, -32, 45, -33, 42, -34, 31 }));
        AddState(133, new State(new int[] { 13, 134, 27, 27, 22, 40 }));
        AddState(134, new State(-10));
        AddState(135, new State(new int[] { 30, 136, 31, 138 }));
        AddState(136, new State(new int[] { 3, 137 }));
        AddState(137, new State(-20));
        AddState(138, new State(new int[] { 4, 139 }));
        AddState(139, new State(new int[] { 32, 140 }));
        AddState(140, new State(new int[] { 7, 46, 5, 47, 6, 48, 8, 49, 10, 50, 9, 51 }, new int[] { -9, 141 }));
        AddState(141, new State(-16));
        AddState(142, new State(-9, new int[] { -5, 143 }));
        AddState(143, new State(new int[] { 29, 129, 28, -6, 13, -6, 4, -6, 34, -6, 33, -6 }, new int[] { -6, 144 }));
        AddState(144, new State(new int[] { 28, 122, 13, 90, 4, 91, 34, 100, 33, -25 }, new int[] { -13, 145, -18, 121, -19, 95, -23, 96, -20, 106, -21, 107, -22, 113 }));
        AddState(145, new State(new int[] { 33, 146, 13, 90, 4, 91, 34, 100 }, new int[] { -18, 89, -19, 95, -23, 96, -20, 106, -21, 107, -22, 113 }));
        AddState(146, new State(new int[] { 13, 147, 14, 32, 26, 33, 15, 34, 12, 35, 4, 36, 23, 37, 25, 43 }, new int[] { -10, 151, -32, 45, -33, 42, -34, 31 }));
        AddState(147, new State(new int[] { 16, 148 }));
        AddState(148, new State(new int[] { 31, 149 }));
        AddState(149, new State(new int[] { 3, 150 }));
        AddState(150, new State(-13));
        AddState(151, new State(new int[] { 13, 152, 27, 27, 22, 40 }));
        AddState(152, new State(new int[] { 16, 153 }));
        AddState(153, new State(new int[] { 31, 154 }));
        AddState(154, new State(new int[] { 3, 155 }));
        AddState(155, new State(-14));
        AddState(156, new State(-17, new int[] { -15, 157 }));
        AddState(157, new State(new int[] { 3, 158, 32, 159 }));
        AddState(158, new State(-15));
        AddState(159, new State(new int[] { 7, 46, 5, 47, 6, 48, 8, 49, 10, 50, 9, 51 }, new int[] { -9, 160 }));
        AddState(160, new State(new int[] { 4, 161 }));
        AddState(161, new State(-18));
        AddState(162, new State(-4));

        rules = new Rule[70];
        rules[1] = new Rule(-2, new int[] { -1, 2 });
        rules[2] = new Rule(-1, new int[] { -3 });
        rules[3] = new Rule(-1, new int[] { -4 });
        rules[4] = new Rule(-1, new int[] { 1 });
        rules[5] = new Rule(-4, new int[] { -5, -6, -7, -8 });
        rules[6] = new Rule(-6, new int[] { });
        rules[7] = new Rule(-6, new int[] { -6, 28, -9, 4, 13 });
        rules[8] = new Rule(-6, new int[] { -6, 28, -9, 4, 11, -10, 13 });
        rules[9] = new Rule(-5, new int[] { });
        rules[10] = new Rule(-5, new int[] { -5, 29, -9, 4, 11, -10, 13 });
        rules[11] = new Rule(-7, new int[] { });
        rules[12] = new Rule(-7, new int[] { -7, -11 });
        rules[13] = new Rule(-11, new int[] { -12, -5, -6, -13, 33, 13, 16, 31, 3 });
        rules[14] = new Rule(-11, new int[] { -12, -5, -6, -13, 33, -10, 13, 16, 31, 3 });
        rules[15] = new Rule(-12, new int[] { -14, -15, 3 });
        rules[16] = new Rule(-14, new int[] { 3, 31, 4, 32, -9 });
        rules[17] = new Rule(-15, new int[] { });
        rules[18] = new Rule(-15, new int[] { -15, 32, -9, 4 });
        rules[19] = new Rule(-8, new int[] { -16, -5, -6, -13, 16, 30, 3 });
        rules[20] = new Rule(-16, new int[] { 3, 30, 3 });
        rules[21] = new Rule(-17, new int[] { -18 });
        rules[22] = new Rule(-17, new int[] { 46, -13, 48 });
        rules[23] = new Rule(-13, new int[] { -18 });
        rules[24] = new Rule(-13, new int[] { -13, -18 });
        rules[25] = new Rule(-18, new int[] { });
        rules[26] = new Rule(-18, new int[] { 13 });
        rules[27] = new Rule(-18, new int[] { 4, 11, -10, 13 });
        rules[28] = new Rule(-18, new int[] { -19 });
        rules[29] = new Rule(-18, new int[] { -20 });
        rules[30] = new Rule(-20, new int[] { -21, 13 });
        rules[31] = new Rule(-20, new int[] { -22, 13 });
        rules[32] = new Rule(-21, new int[] { -22, 4, 11, -10 });
        rules[33] = new Rule(-21, new int[] { -21, 32, 4, 11, -10 });
        rules[34] = new Rule(-22, new int[] { 4 });
        rules[35] = new Rule(-23, new int[] { 34, 4, 11, -10, 35, -10 });
        rules[36] = new Rule(-19, new int[] { -23, 36, -17 });
        rules[37] = new Rule(-3, new int[] { -24 });
        rules[38] = new Rule(-3, new int[] { -3, -24 });
        rules[39] = new Rule(-24, new int[] { -25, -26, -27, 15, 13, -28, -29 });
        rules[40] = new Rule(-24, new int[] { -30, -26, -27, 15, 13, -28, -29 });
        rules[41] = new Rule(-25, new int[] { -30, 4, -9, 13 });
        rules[42] = new Rule(-25, new int[] { -30, 4, -9, 11, -10, 13 });
        rules[43] = new Rule(-25, new int[] { -25, 4, -9, 13 });
        rules[44] = new Rule(-25, new int[] { -25, 4, -9, 11, -10, 13 });
        rules[45] = new Rule(-30, new int[] { 3, 4, 3, -31 });
        rules[46] = new Rule(-31, new int[] { 3, 18, 3 });
        rules[47] = new Rule(-26, new int[] { 16, 18, 3 });
        rules[48] = new Rule(-27, new int[] { 3, 17, 3 });
        rules[49] = new Rule(-28, new int[] { 16, 17, 3 });
        rules[50] = new Rule(-29, new int[] { 16, 19, 3 });
        rules[51] = new Rule(-9, new int[] { 7 });
        rules[52] = new Rule(-9, new int[] { 5 });
        rules[53] = new Rule(-9, new int[] { 6 });
        rules[54] = new Rule(-9, new int[] { 8 });
        rules[55] = new Rule(-9, new int[] { 10 });
        rules[56] = new Rule(-9, new int[] { 9 });
        rules[57] = new Rule(-10, new int[] { -10, 27, -32 });
        rules[58] = new Rule(-10, new int[] { -10, 22, -32 });
        rules[59] = new Rule(-10, new int[] { -32 });
        rules[60] = new Rule(-32, new int[] { -32, 21, -33 });
        rules[61] = new Rule(-32, new int[] { -33 });
        rules[62] = new Rule(-33, new int[] { -34 });
        rules[63] = new Rule(-33, new int[] { 23, -10, 24 });
        rules[64] = new Rule(-33, new int[] { 25, -33 });
        rules[65] = new Rule(-34, new int[] { 14 });
        rules[66] = new Rule(-34, new int[] { 26 });
        rules[67] = new Rule(-34, new int[] { 15 });
        rules[68] = new Rule(-34, new int[] { 12 });
        rules[69] = new Rule(-34, new int[] { 4 });

        nonTerminals = new string[] {"", "Script", "$accept", "EntryList",
     "Main", "ConstDecls", "VarDecls", "FuncList", "MainDecl", "TypeName", "Expr",
     "FuncDecl", "FD2", "InstrBlock", "FD1", "ParamList", "MD1", "InstrList",
     "Instruction", "ForLoop", "KeyWord", "KW1", "KW2", "FL1", "Entry", "M2",
     "InParamEnd", "CodeNameBegin", "CodeNameEnd", "ActionEnd", "M1", "InParamBegin",
     "Term", "Fact", "Value", };
    }

    protected override void DoAction(int action)
    {
        switch (action)
        {
            case 4: // Script -> error
                { // blad kompilacji
                    //yyerrok();
                    throw new Exception("Compilation asfsdfsdferror");
                }
                break;
            case 5: // Main -> ConstDecls VarDecls FuncList MainDecl
                {
                    string code = value_stack.array[value_stack.top - 4].value + value_stack.array[value_stack.top - 3].value;
                    Interpreter.mCSCodeFields.Add(code);
                    code = value_stack.array[value_stack.top - 2].value;
                    Interpreter.mCSCodeMethods.Add(code);
                    Interpreter.mCSCodeUpdate.Add(value_stack.array[value_stack.top - 1].value);
                    Interpreter.mCSCodeInitialize.Add("/*not yet*/");
                }
                break;
            case 6: // VarDecls -> /* empty */
                {
                    yyval.value = "";
                }
                break;
            case 7: // VarDecls -> VarDecls qKeyVar TypeName qIdentifier qSemicolon
                {
                    // zmianna do tablicy symboli
                    CVarInfo var = new CVarInfo(value_stack.array[value_stack.top - 2].value, value_stack.array[value_stack.top - 3].value);
                    var.DeclarationLvl = declLvl;
                    Interpreter.mST.Add(var);

                    // jesli deklaracja wew. funkcji
                    if (actFunc != null)
                        actFunc.AddVar(var);

                    // tlumaczenie do c#
                    string vt = value_stack.array[value_stack.top - 3].value;
                    if (value_stack.array[value_stack.top - 3].value == "real")
                        vt = "float";
                    yyval.value = value_stack.array[value_stack.top - 5].value + vt + " " + value_stack.array[value_stack.top - 2].value + ";\n";
                }
                break;
            case 8: // VarDecls -> VarDecls qKeyVar TypeName qIdentifier qSet Expr qSemicolon
                {
                    // zmianna do tablicy symboli
                    CVarInfo var = new CVarInfo(value_stack.array[value_stack.top - 4].value, value_stack.array[value_stack.top - 5].value);
                    var.SetVal(value_stack.array[value_stack.top - 2].value, var.VarType);
                    var.DeclarationLvl = declLvl;
                    Interpreter.mST.Add(var);

                    // jesli deklaracja wew. funkcji
                    if (actFunc != null)
                        actFunc.AddVar(var);

                    // tlumaczenie do c#
                    string vt = value_stack.array[value_stack.top - 5].value;
                    if (value_stack.array[value_stack.top - 5].value == "real")
                        vt = "float";
                    yyval.value = value_stack.array[value_stack.top - 7].value + vt + " " + value_stack.array[value_stack.top - 4].value + " = " + value_stack.array[value_stack.top - 2].value.ToString() + ";\n";
                }
                break;
            case 9: // ConstDecls -> /* empty */
                {
                    yyval.value = "";
                }
                break;
            case 10: // ConstDecls -> ConstDecls qKeyConst TypeName qIdentifier qSet Expr qSemicolon
                {
                    // zmianna do tablicy symboli
                    CVarInfo var = new CVarInfo(value_stack.array[value_stack.top - 4].value, value_stack.array[value_stack.top - 5].value);
                    var.SetVal(value_stack.array[value_stack.top - 2].value, var.VarType);
                    var.IsConst = true;
                    var.DeclarationLvl = declLvl;
                    Interpreter.mST.Add(var);

                    // jesli deklaracja wew. funkcji
                    if (actFunc != null)
                        actFunc.AddVar(var);

                    // tlumaczenie do c#
                    string vt = value_stack.array[value_stack.top - 5].value;
                    if (value_stack.array[value_stack.top - 5].value == "real")
                        vt = "float";
                    yyval.value = value_stack.array[value_stack.top - 7].value + vt + " " + value_stack.array[value_stack.top - 4].value + " = " + value_stack.array[value_stack.top - 2].value.ToString() + ";\n";
                }
                break;
            case 11: // FuncList -> /* empty */
                {
                    yyval.value = "";
                }
                break;
            case 12: // FuncList -> FuncList FuncDecl
                {
                    // c# - laczymy kolejne funkcje
                    yyval.value = value_stack.array[value_stack.top - 2].value + value_stack.array[value_stack.top - 1].value;
                }
                break;
            case 13: // FuncDecl -> FD2 ConstDecls VarDecls InstrBlock qRet qSemicolon qXml qKeyFunction qRel
                {
                    yyval.value = value_stack.array[value_stack.top - 9].value + value_stack.array[value_stack.top - 8].value + value_stack.array[value_stack.top - 7].value + value_stack.array[value_stack.top - 6].value + value_stack.array[value_stack.top - 5].value + ";\n}\n";
                }
                break;
            case 14: // FuncDecl -> FD2 ConstDecls VarDecls InstrBlock qRet Expr qSemicolon qXml qKeyFunction qRel
                {
                    yyval.value = value_stack.array[value_stack.top - 10].value + value_stack.array[value_stack.top - 9].value + value_stack.array[value_stack.top - 8].value + value_stack.array[value_stack.top - 7].value + value_stack.array[value_stack.top - 6].value + " " + value_stack.array[value_stack.top - 5].value + ";\n}\n";
                }
                break;
            case 15: // FD2 -> FD1 ParamList qRel
                {
                    int idx = value_stack.array[value_stack.top - 2].value.Length - 2;
                    if (idx >= 0)
                        value_stack.array[value_stack.top - 2].value = value_stack.array[value_stack.top - 2].value.Remove(idx);
                    yyval.value = value_stack.array[value_stack.top - 3].value + value_stack.array[value_stack.top - 2].value + "){\n";

                }
                break;
            case 16: // FD1 -> qRel qKeyFunction qIdentifier qComma TypeName
                {
                    // tablica symboli
                    CFuncInfo func = new CFuncInfo(value_stack.array[value_stack.top - 3].value, value_stack.array[value_stack.top - 1].value);
                    Interpreter.mST.Add(func);

                    // stan parsera
                    actFunc = func;
                    declLvl = DecLvl.DL_Function;

                    // tlumaczenie
                    string vt = value_stack.array[value_stack.top - 1].value;
                    if (vt == "real")
                        vt = "float";
                    yyval.value = "public " + vt + " " + value_stack.array[value_stack.top - 3].value + "(";
                }
                break;
            case 17: // ParamList -> /* empty */
                {
                    yyval.value = "";
                }
                break;
            case 18: // ParamList -> ParamList qComma TypeName qIdentifier
                {
                    // do tablicy symboli
                    CVarInfo param = new CVarInfo(value_stack.array[value_stack.top - 1].value, value_stack.array[value_stack.top - 2].value);
                    param.IsParam = true;
                    actFunc.AddArg(param);
                    Interpreter.mST.Add(param);

                    // tlumaczenie do C#
                    string vt = value_stack.array[value_stack.top - 2].value;
                    if (vt == "real")
                        vt = "float";
                    yyval.value = value_stack.array[value_stack.top - 4].value + " " + vt + " " + value_stack.array[value_stack.top - 1].value + ", ";
                }
                break;
            case 19: // MainDecl -> MD1 ConstDecls VarDecls InstrBlock qXml qKeyMain qRel
                {
                    yyval.value = value_stack.array[value_stack.top - 7].value + value_stack.array[value_stack.top - 6].value + value_stack.array[value_stack.top - 5].value + value_stack.array[value_stack.top - 4].value + "\n}\n";
                }
                break;
            case 20: // MD1 -> qRel qKeyMain qRel
                {
                    // tablica symboli
                    CFuncInfo func = new CFuncInfo("update", "void");
                    Interpreter.mST.Add(func);

                    // stan parsera
                    actFunc = func;
                    declLvl = DecLvl.DL_Function;

                    // tlumaczenie
                    yyval.value = "public void Update (){\n";
                }
                break;
            case 21: // InstrList -> Instruction
                {
                    // C#
                    yyval.value = value_stack.array[value_stack.top - 1].value;
                }
                break;
            case 22: // InstrList -> qLeft5 InstrBlock qRight5
                {
                    // c#
                    yyval.value = "{\n" + value_stack.array[value_stack.top - 2].value + "}\n";
                }
                break;
            case 23: // InstrBlock -> Instruction
                {
                    // c#
                    yyval.value = value_stack.array[value_stack.top - 1].value;
                }
                break;
            case 24: // InstrBlock -> InstrBlock Instruction
                {
                    // c#
                    yyval.value = value_stack.array[value_stack.top - 2].value + value_stack.array[value_stack.top - 1].value;
                }
                break;
            case 25: // Instruction -> /* empty */
                {
                    yyval.value = "";
                }
                break;
            case 26: // Instruction -> qSemicolon
                {
                    //c#
                    yyval.value = ";\n";
                }
                break;
            case 27: // Instruction -> qIdentifier qSet Expr qSemicolon
                {
                    // sprawdzenie tablicy symboli
                    CVarInfo var = (CVarInfo)Interpreter.mST.Get(value_stack.array[value_stack.top - 4].value);
                    if (var == null)
                        throw new Exception("variable not declared");

                    if (value_stack.array[value_stack.top - 4].type != value_stack.array[value_stack.top - 2].type)
                        throw new Exception("variable not declared");

                    // c#
                    yyval.value = value_stack.array[value_stack.top - 4].value + " = " + value_stack.array[value_stack.top - 2].value + ";\n";
                }
                break;
            case 28: // Instruction -> ForLoop
                {
                    yyval.value = value_stack.array[value_stack.top - 1].value;
                }
                break;
            case 29: // Instruction -> KeyWord
                {
                    yyval.value = value_stack.array[value_stack.top - 1].value;
                }
                break;
            case 30: // KeyWord -> KW1 qSemicolon
                {
                    yyval.value = value_stack.array[value_stack.top - 2].value + ");\n";
                }
                break;
            case 31: // KeyWord -> KW2 qSemicolon
                {
                    yyval.value = value_stack.array[value_stack.top - 2].value + ");\n";
                }
                break;
            case 32: // KW1 -> KW2 qIdentifier qSet Expr
                {
                    CVarInfo var = (CVarInfo)recInfo.VarList[IntTemp++];
                    if (var.Ident != value_stack.array[value_stack.top - 3].value)
                        throw new Exception("bad parameter");

                    if (var.VarType != value_stack.array[value_stack.top - 1].type)
                        throw new Exception("bad parameter");

                    yyval.value = value_stack.array[value_stack.top - 4].value + value_stack.array[value_stack.top - 1].value;
                }
                break;
            case 33: // KW1 -> KW1 qComma qIdentifier qSet Expr
                {
                    CVarInfo var = (CVarInfo)recInfo.VarList[IntTemp++];
                    if (var.Ident != value_stack.array[value_stack.top - 3].value)
                        throw new Exception("bad parameter");
                    if (var.VarType != value_stack.array[value_stack.top - 1].type)
                        throw new Exception("bad parameter");

                    yyval.value = value_stack.array[value_stack.top - 5].value + ", " + value_stack.array[value_stack.top - 1].value;
                }
                break;
            case 34: // KW2 -> qIdentifier
                {
                    CRecordInfo var = (CRecordInfo)Interpreter.mST.Get(value_stack.array[value_stack.top - 1].value);
                    if (var == null)
                        throw new Exception("variable not declared");
                    recInfo = var;
                    IntTemp = 0;

                    for (int i = 0; i < var.VarList.Count; i++)
                    {
                        CVarInfo vi = (CVarInfo)var.VarList[i];
                        if (vi.Ident == "codename")
                            yyval.value = vi.StringVal.Replace("\"", " ") + "( ";
                    }
                }
                break;
            case 35: // FL1 -> qFor qIdentifier qSet Expr qFor2 Expr
                {
                    CVarInfo var = (CVarInfo)Interpreter.mST.Get(value_stack.array[value_stack.top - 5].value);
                    if (var == null)
                        throw new Exception("variable not declared");

                    if (var.VarType != EVarType.VT_Int || value_stack.array[value_stack.top - 3].type != EVarType.VT_Int || value_stack.array[value_stack.top - 1].type != EVarType.VT_Int)
                        throw new Exception("bad type in for loop");

                    string toSem, toSem2;
                    if (value_stack.array[value_stack.top - 2].value == "to")
                    {
                        toSem = "<=";
                        toSem2 = "++";
                    }
                    else
                    {
                        toSem = ">=";
                        toSem2 = "--";
                    }

                    yyval.value = "for( " + value_stack.array[value_stack.top - 5].value + " = " + value_stack.array[value_stack.top - 3].value + "; " + value_stack.array[value_stack.top - 5].value + toSem + value_stack.array[value_stack.top - 1].value + "; " + value_stack.array[value_stack.top - 5].value + toSem2 + ")\n";
                }
                break;
            case 36: // ForLoop -> FL1 qDo InstrList
                {
                    yyval.value = value_stack.array[value_stack.top - 3].value + value_stack.array[value_stack.top - 1].value;
                }
                break;
            case 39: // Entry -> M2 InParamEnd CodeNameBegin qValString qSemicolon CodeNameEnd ActionEnd
                {       // tutaj wstawiamy docelowa funkcje
                    CVarInfo var = new CVarInfo("codename", "string");
                    var.StringVal = value_stack.array[value_stack.top - 4].value;
                    CRecordInfo rec = (CRecordInfo)Interpreter.mST.Get(temp);
                    rec.AddVar(var);
                }
                break;
            case 40: // Entry -> M1 InParamEnd CodeNameBegin qValString qSemicolon CodeNameEnd ActionEnd
                {       // tutaj wstawiamy docelowa funkcje
                    CVarInfo var = new CVarInfo("codename", "string");
                    var.StringVal = value_stack.array[value_stack.top - 4].value;
                    CRecordInfo rec = (CRecordInfo)Interpreter.mST.Get(temp);
                    rec.AddVar(var);
                }
                break;
            case 41: // M2 -> M1 qIdentifier TypeName qSemicolon
                {
                    CRecordInfo rec = (CRecordInfo)Interpreter.mST.Get(temp);
                    CVarInfo var = new CVarInfo(value_stack.array[value_stack.top - 3].value, value_stack.array[value_stack.top - 2].value);
                    rec.AddVar(var);
                }
                break;
            case 42: // M2 -> M1 qIdentifier TypeName qSet Expr qSemicolon
                {       // tu wstawiamy kolejne zmienne do rekordu
                    CRecordInfo rec = (CRecordInfo)Interpreter.mST.Get(temp);
                    CVarInfo var = new CVarInfo(value_stack.array[value_stack.top - 5].value, value_stack.array[value_stack.top - 4].value);
                    switch (var.VarType)
                    {
                        case EVarType.VT_Int:
                            var.IntVal = Int32.Parse(value_stack.array[value_stack.top - 2].value);
                            break;
                        case EVarType.VT_Real:
                            value_stack.array[value_stack.top - 2].value = value_stack.array[value_stack.top - 2].value.Replace(".", ",");
                            var.RealVal = (float)Double.Parse(value_stack.array[value_stack.top - 2].value);
                            break;
                        case EVarType.VT_Bool:
                            var.BoolVal = Boolean.Parse(value_stack.array[value_stack.top - 2].value);
                            break;
                        case EVarType.VT_String:
                            var.StringVal = value_stack.array[value_stack.top - 2].value;
                            break;
                        default:
                            break;
                    }
                    rec.AddVar(var);
                }
                break;
            case 43: // M2 -> M2 qIdentifier TypeName qSemicolon
                {
                    CRecordInfo rec = (CRecordInfo)Interpreter.mST.Get(temp);
                    CVarInfo var = new CVarInfo(value_stack.array[value_stack.top - 3].value, value_stack.array[value_stack.top - 2].value);
                    rec.AddVar(var);
                }
                break;
            case 44: // M2 -> M2 qIdentifier TypeName qSet Expr qSemicolon
                {
                    CRecordInfo rec = (CRecordInfo)Interpreter.mST.Get(temp);
                    CVarInfo var = new CVarInfo(value_stack.array[value_stack.top - 5].value, value_stack.array[value_stack.top - 4].value);
                    switch (var.VarType)
                    {
                        case EVarType.VT_Int:
                            var.IntVal = Int32.Parse(value_stack.array[value_stack.top - 2].value);
                            break;
                        case EVarType.VT_Real:
                            value_stack.array[value_stack.top - 2].value = value_stack.array[value_stack.top - 2].value.Replace(".", ",");
                            var.RealVal = (float)Double.Parse(value_stack.array[value_stack.top - 2].value);
                            break;
                        case EVarType.VT_Bool:
                            var.BoolVal = Boolean.Parse(value_stack.array[value_stack.top - 2].value);
                            break;
                        case EVarType.VT_String:
                            var.StringVal = value_stack.array[value_stack.top - 2].value;
                            break;
                        default:
                            break;
                    }
                    rec.AddVar(var);
                }
                break;
            case 45: // M1 -> qRel qIdentifier qRel InParamBegin
                {
                    CRecordInfo entry = new CRecordInfo(value_stack.array[value_stack.top - 3].value, null);
                    Interpreter.mST.Add(entry);
                    temp = value_stack.array[value_stack.top - 3].value;
                }
                break;
            case 46: // InParamBegin -> qRel qKeyParam qRel
                {

                }
                break;
            case 51: // TypeName -> qReal
                { yyval.value = value_stack.array[value_stack.top - 1].value; }
                break;
            case 52: // TypeName -> qBool
                { yyval.value = value_stack.array[value_stack.top - 1].value; }
                break;
            case 53: // TypeName -> qInt
                { yyval.value = value_stack.array[value_stack.top - 1].value; }
                break;
            case 54: // TypeName -> qString
                { yyval.value = value_stack.array[value_stack.top - 1].value; }
                break;
            case 55: // TypeName -> qChar
                { yyval.value = value_stack.array[value_stack.top - 1].value; }
                break;
            case 56: // TypeName -> qVoid
                { yyval.value = value_stack.array[value_stack.top - 1].value; }
                break;
            case 57: // Expr -> Expr qAdd Term
                {
                    switch (value_stack.array[value_stack.top - 2].value)
                    {
                        case "+":
                            if (value_stack.array[value_stack.top - 3].type != value_stack.array[value_stack.top - 1].type)
                                throw new Exception("Wrong type");

                            yyval.value = value_stack.array[value_stack.top - 3].value + " + " + value_stack.array[value_stack.top - 1].value;
                            yyval.type = value_stack.array[value_stack.top - 3].type;
                            break;

                        case "-":
                            if (value_stack.array[value_stack.top - 3].type != value_stack.array[value_stack.top - 1].type)
                                throw new Exception("Wrong type");

                            yyval.value = value_stack.array[value_stack.top - 3].value + " - " + value_stack.array[value_stack.top - 1].value;
                            yyval.type = value_stack.array[value_stack.top - 3].type;
                            break;
                    }
                }
                break;
            case 58: // Expr -> Expr qOr Term
                {
                    if (value_stack.array[value_stack.top - 3].type != value_stack.array[value_stack.top - 1].type)
                        throw new Exception("Wrong type");

                    yyval.value = value_stack.array[value_stack.top - 3].value + " || " + value_stack.array[value_stack.top - 1].value;
                    yyval.type = value_stack.array[value_stack.top - 3].type;
                }
                break;
            case 59: // Expr -> Term
                {
                    yyval = value_stack.array[value_stack.top - 1];
                }
                break;
            case 60: // Term -> Term qMul Fact
                {
                    switch (value_stack.array[value_stack.top - 2].value)
                    {
                        case "*":
                            if (value_stack.array[value_stack.top - 3].type != value_stack.array[value_stack.top - 1].type)
                                throw new Exception("Wrong type");

                            yyval.value = value_stack.array[value_stack.top - 3].value + " * " + value_stack.array[value_stack.top - 1].value;
                            yyval.type = value_stack.array[value_stack.top - 3].type;
                            break;
                        case "/":
                            if (value_stack.array[value_stack.top - 3].type != value_stack.array[value_stack.top - 1].type)
                                throw new Exception("Wrong type");

                            yyval.value = value_stack.array[value_stack.top - 3].value + " / " + value_stack.array[value_stack.top - 1].value;
                            yyval.type = value_stack.array[value_stack.top - 3].type;
                            break;
                        case "and":
                            if (value_stack.array[value_stack.top - 3].type != value_stack.array[value_stack.top - 1].type)
                                throw new Exception("Wrong type");

                            yyval.value = value_stack.array[value_stack.top - 3].value + " && " + value_stack.array[value_stack.top - 1].value;
                            yyval.type = value_stack.array[value_stack.top - 3].type;
                            break;
                    }
                }
                break;
            case 61: // Term -> Fact
                {
                    yyval = value_stack.array[value_stack.top - 1];
                }
                break;
            case 62: // Fact -> Value
                {
                    yyval = value_stack.array[value_stack.top - 1];
                }
                break;
            case 63: // Fact -> qLeft1 Expr qRight1
                {
                    yyval.value = " ( " + value_stack.array[value_stack.top - 2].value + " ) ";
                    yyval.type = value_stack.array[value_stack.top - 2].type;
                }
                break;
            case 64: // Fact -> qNot Fact
                {
                    if (value_stack.array[value_stack.top - 1].type != EVarType.VT_Bool && value_stack.array[value_stack.top - 1].type != EVarType.VT_Int)
                        throw new Exception("Wrong type used");

                    yyval.value = "!" + value_stack.array[value_stack.top - 1].value;
                    yyval.type = value_stack.array[value_stack.top - 1].type;
                }
                break;
            case 65: // Value -> qValInt
                { yyval.value = value_stack.array[value_stack.top - 1].value; yyval.type = EVarType.VT_Int; }
                break;
            case 66: // Value -> qValReal
                { yyval.value = value_stack.array[value_stack.top - 1].value + "f"; yyval.type = EVarType.VT_Real; }
                break;
            case 67: // Value -> qValString
                { yyval.value = value_stack.array[value_stack.top - 1].value; yyval.type = EVarType.VT_String; }
                break;
            case 68: // Value -> qLogical
                { yyval.value = value_stack.array[value_stack.top - 1].value; yyval.type = EVarType.VT_Bool; }
                break;
            case 69: // Value -> qIdentifier
                {
                    // sprawdzamy tablice symboli
                    CVarInfo var = (CVarInfo)Interpreter.mST.Get(value_stack.array[value_stack.top - 1].value);
                    if (var == null)
                        throw new Exception("variable undeclared");


                    yyval.value = value_stack.array[value_stack.top - 1].value;
                    yyval.type = var.VarType;
                }
                break;
        }
    }

    protected override string TerminalToString(int terminal)
    {
        if (((Tokens)terminal).ToString() != terminal.ToString())
            return ((Tokens)terminal).ToString();
        else
            return CharToString((char)terminal);
    }



    public class Scanner : gppg.IScanner<SemInfo, LexLocation>
    {
        private Analex lex;
        private LexLocation loc;

        public Scanner(string fname)
        {
            lex = new Analex();
            lex.Init(fname);
            loc = new LexLocation();
        }

        public override LexLocation yylloc
        {
            get { return loc; }
            set { loc = value; }
        }

        public override int yylex()
        {
            SemanticInfo ret = lex.Analize(loc.sLin);
            yylval.value = ret.tokenVal;
            return ret.tokenId;
        }

        public override void yyerror(string format, params object[] args)
        {
            Console.Error.WriteLine(format, args);
        }
    }
}
