Esempio n. 1
0
        static bool ActionOnProcessTempDeclarator(SyntaxStack ss, Production pp)
        {
            SDeclTypeAttribute sp = ss.ss[ss.sp - 1] as SDeclTypeAttribute; // Declarator
            IDeclTypeAttribute ip = ss.ss[ss.sp - 4] as IDeclTypeAttribute; // Direct_Declarator_Tail
            Type ty = null, tty = null;

            for (int _j = 0; _j < sp.np; _j++)
            {
                tty = BuildType(TypeOperator.POINTER, tty, 4, null);
            }
            for (int _j = 0; _j < sp.stype.Count; _j++)
            {
                ty  = sp.stype.Pop();
                tty = BuildType(ty.op, tty, ty.size, ty.lsym);
            }
            ty = sp.tmpty;
            while (ty != null && ty.type != null)
            {
                ty = ty.type;
            }

            if (sp.tmpty == null)
            {
                sp.tmpty = tty;
            }
            else
            {
                ty.type = tty;
            }

            ip.tmpty = sp.tmpty;

            ip.id = sp.id;
            return(true);
        }
Esempio n. 2
0
        static bool ActionOnExpandParameterList(SyntaxStack ss, Production pp, string token)
        {
            List <Symbol>      lsym = new List <Symbol>();
            IDeclTypeAttribute sp   = ss.ss[ss.sp - 1] as IDeclTypeAttribute;
            IDeclTypeAttribute dp   = null;

            switch (pp.prodno)
            {
            case (27):     // Parameter_List_Tail -> Comma Parameter_Declaration Parameter_List_Tail
                dp = pp.inh[2] as IDeclTypeAttribute;
                foreach (Symbol sym in sp.lsym)
                {
                    dp.lsym.Add(sym);
                }
                break;      // Parameter_List_Tail ->Lambda

            case (28):
                dp = ss.ss[ss.sp - 4] as IDeclTypeAttribute;     // dump symbols to Direct_Declarator_Tail
                foreach (Symbol sym in sp.lsym)
                {
                    lsym.Add(sym);
                }

                dp.stype.Push(BuildType(TypeOperator.FUNCTION, null, 0, lsym));
                break;
            }
            return(true);
        }
Esempio n. 3
0
        static bool ActionOnLoadIdToDirectDeclTail(SyntaxStack ss, Production pp, string token)
        {
            IDeclTypeAttribute sp = ss.ss[ss.sp - 3] as IDeclTypeAttribute; // Direct_declarator_Tail

            sp.id = token.ToUpper();
            return(true);
        }
Esempio n. 4
0
        static bool ActionOnLoadNumToDirectDeclTail(SyntaxStack ss, Production pp, string token)
        {
            IDeclTypeAttribute sp = ss.ss[ss.sp - 4] as IDeclTypeAttribute; // Direct_declarator_Tail

            sp.stype.Push(BuildType(TypeOperator.ARRAY, null, Convert.ToInt16(token), null));
            return(true);
        }
Esempio n. 5
0
        static bool BuildPointerType(SyntaxStack ss, Production pp, string token)
        {
            IDeclTypeAttribute sp = ss.ss[ss.sp - 4] as IDeclTypeAttribute;

            sp.basety = BuildType(TypeOperator.POINTER, sp.basety, 4, null);

            return(true);
        }
Esempio n. 6
0
        static bool ActionLoadSignSpecifier(SyntaxStack ss, Production pp)
        {
            IDeclTypeAttribute ip = ss.ss[ss.sp - 5] as IDeclTypeAttribute;
            SDeclTypeAttribute sp = ss.ss[ss.sp - 1] as SDeclTypeAttribute;

            ip.signed = sp.signed;
            return(true);
        }
Esempio n. 7
0
        static bool ActionLoadStorageClassModifier(SyntaxStack ss, Production pp)
        {
            IDeclTypeAttribute ip = ss.ss[ss.sp - 7] as IDeclTypeAttribute;
            SDeclTypeAttribute sp = ss.ss[ss.sp - 1] as SDeclTypeAttribute;

            ip.sclass = sp.sclass;
            return(true);
        }
Esempio n. 8
0
        static bool ActionOnExpandDeclarator(SyntaxStack ss, Production pp, string token)
        {
            IDeclTypeAttribute sp = ss.ss[ss.sp - 1] as IDeclTypeAttribute; //Declarator
            IDeclTypeAttribute dp = null;

            dp        = pp.inh[0] as IDeclTypeAttribute; // Pointer
            dp.basety = sp.basety;
            dp.sclass = sp.sclass;
            return(true);
        }
Esempio n. 9
0
        static bool ActionOnLoadParameterList(SyntaxStack ss, Production pp)
        {
            SDeclTypeAttribute sp = ss.ss[ss.sp - 1] as SDeclTypeAttribute;
            IDeclTypeAttribute ip = ss.ss[ss.sp - 3] as IDeclTypeAttribute;
            Type ty;
            int  nitems = 0;

            for (int _j = 0; _j < sp.np; _j++)
            {
                sp.basety = BuildType(TypeOperator.POINTER, sp.basety, 4, null);
            }

            nitems = sp.stype.Count;
            for (int _j = 0; _j < nitems; _j++)
            {
                ty        = sp.stype.Pop();
                sp.basety = BuildType(ty.op, sp.basety, ty.size, ty.lsym);
            }

            ty = sp.tmpty;
            while (ty != null && ty.type != null)
            {
                ty = ty.type;
            }
            if (ty != null)
            {
                ty.type   = sp.basety;
                sp.basety = sp.tmpty;
            }

            // create a sym
            Symbol sym = new Symbol();

            sym.name   = sp.id;
            sym.sclass = sp.sclass;
            sym.type   = sp.basety;

            if (ScanParameterList(ip.lsym, sym.name))
            {
                ip.lsym.Add(sym);
            }
            else
            {
                ErrMsg = "duplicate parameter name : " + sym.name;
                return(false);
            }

            return(true);
        }
Esempio n. 10
0
        static bool ActionOnExpandParameterDeclarationTail(SyntaxStack ss, Production pp, string token)
        {
            IDeclTypeAttribute sp = ss.ss[ss.sp - 1] as IDeclTypeAttribute;
            IDeclTypeAttribute dp = null;

            switch (pp.prodno)
            {
            case (30):     // Parameter_Declaration_Tail -> Declarator
                dp        = pp.inh[0] as IDeclTypeAttribute;
                dp.basety = sp.basety;
                dp.sclass = sp.sclass;
                break;
            }
            return(true);
        }
Esempio n. 11
0
        static bool ActionLoadBaseType(SyntaxStack ss, Production pp)
        {
            IDeclTypeAttribute ip = ss.ss[ss.sp - 3] as IDeclTypeAttribute; // Declarator
            SDeclTypeAttribute sp = ss.ss[ss.sp - 1] as SDeclTypeAttribute; // Type_specifier

            switch (sp.basety.op)
            {
            case (TypeOperator.VOID):
            case (TypeOperator.CHAR):
            case (TypeOperator.INT):
            case (TypeOperator.LONG):
                ip.basety        = sp.basety;
                ip.basety.signed = ip.signed;
                break;
            }
            return(true);
        }
Esempio n. 12
0
        static bool ActionOnExpandParameterTypeList(SyntaxStack ss, Production pp, string token)
        {
            IDeclTypeAttribute dp = null;

            switch (pp.prodno)
            {
            case (24):
                dp = ss.ss[ss.sp - 4] as IDeclTypeAttribute;
                break;

            case (25):
                dp = ss.ss[ss.sp - 4] as IDeclTypeAttribute;                    // dump empty arg list to Direct_Declarator_Tail
                dp.stype.Push(BuildType(TypeOperator.FUNCTION, null, 0, null)); // no argument
                break;
            }
            return(true);
        }
Esempio n. 13
0
        public IDeclTypeAttribute(IDeclTypeAttribute ip)
        {
            nTerm  = ip.nTerm;
            ErrMsg = ip.ErrMsg;
            sym    = ip.sym;
            basety = ip.basety;
            tmpty  = ip.tmpty;
            signed = ip.signed;
            sclass = ip.sclass;
            size   = ip.size;
            id     = ip.id;
            np     = ip.np;
            foreach (Symbol ssym in ip.lsym)
            {
                lsym.Add(ssym);
            }

            __Copy_Stack(stype, ip.stype);
        }
Esempio n. 14
0
        static bool ActionOnExpandDirectDeclTail(SyntaxStack ss, Production pp, string token)
        {
            IDeclTypeAttribute sp = ss.ss[ss.sp - 1] as IDeclTypeAttribute; // Direct_Declarator_Tail
            IDeclTypeAttribute ip = null;

            switch (pp.prodno)
            {
            case (19):     // Direct_Declarator_Tail -> LQParen Num RQParen Direct_Declator_Tail
                ip        = pp.inh[3] as IDeclTypeAttribute;
                ip.basety = sp.basety;
                ip.tmpty  = sp.tmpty;
                ip.sclass = sp.sclass;
                ip.np     = sp.np;
                ip.id     = sp.id;
                __Copy_Stack(ip.stype, sp.stype);
                break;

            case (20):     // Direct_Declarator_Tail -> LParen Parameter_Type_list RParen Direct_Declator_Tail
                ip        = pp.inh[3] as IDeclTypeAttribute;
                ip.basety = sp.basety;
                ip.tmpty  = sp.tmpty;
                ip.sclass = sp.sclass;
                ip.np     = sp.np;
                ip.id     = sp.id;
                __Copy_Stack(ip.stype, sp.stype);
                break;

            case (21):                                                          // Direct_Declarator_Tail -> Lambda
                SDeclTypeAttribute dp = ss.ss[ss.sp - 2] as SDeclTypeAttribute; // Declarator
                dp.basety = sp.basety;
                dp.tmpty  = sp.tmpty;
                dp.sclass = sp.sclass;
                dp.np     = sp.np;
                dp.id     = sp.id;
                __Copy_Stack(dp.stype, sp.stype);
                break;
            }
            return(true);
        }
Esempio n. 15
0
        static bool ActionOnExpandDirectDecl(SyntaxStack ss, Production pp, string token)
        {
            IDeclTypeAttribute sp = ss.ss[ss.sp - 1] as IDeclTypeAttribute; // Direct_Declarator
            IDeclTypeAttribute ip = null;

            switch (pp.prodno)
            {
            case (17):     // Direct_Declarator -> Id Direct_Declarator_Tail
                ip        = pp.inh[1] as IDeclTypeAttribute;
                ip.basety = sp.basety;
                ip.sclass = sp.sclass;
                ip.np     = sp.np;
                break;

            case (18):     // Direct_Declarator -> LParen Declarator RParen Direct_Declator_Tail
                ip        = pp.inh[3] as IDeclTypeAttribute;
                ip.basety = sp.basety;
                ip.sclass = sp.sclass;
                ip.np     = sp.np;
                break;
            }
            return(true);
        }
Esempio n. 16
0
        static bool ActionOnExpandPointer(SyntaxStack ss, Production pp, string token)
        {
            IDeclTypeAttribute sp = ss.ss[ss.sp - 1] as IDeclTypeAttribute; // Pointer
            IDeclTypeAttribute ip = null;

            switch (pp.prodno)
            {
            case (22):     // Pointer -> Star Pointer
                ip        = pp.inh[1] as IDeclTypeAttribute;
                ip.basety = sp.basety;
                ip.sclass = sp.sclass;
                ip.np     = sp.np;
                ip.np++;
                break;

            case (23):                                              // Pointer -> Lambda (copy rule)
                ip        = ss.ss[ss.sp - 4] as IDeclTypeAttribute; // Direct_Declarator
                ip.basety = sp.basety;
                ip.sclass = sp.sclass;
                ip.np     = sp.np;
                break;
            }
            return(true);
        }
Esempio n. 17
0
        public bool Parse()
        {
            bool       fret = false;
            Production pp   = null;

            object sym = Stack.Top();

            SynthesizedAttribute syn = sym as SynthesizedAttribute;

            if (syn != null)
            {
                if (syn.FireAction != null)
                {
                    fret = syn.FireAction(Stack, pp);
                    if (fret == false)
                    {
                        return(fret);
                    }
                }
                Stack.Pop();
                //DumpStack();
                return(true);
            }

            SemanticAction sma = sym as SemanticAction;

            if (sma != null)
            {
                Stack.Pop();
                sym = Stack.Top();
            }
            InheritedAttribute inh = sym as InheritedAttribute;

            if (inh != null)
            {
                if (token != TokenLookup.TokenKind.UnKnown)
                {
                    pp = (Production)M[(int)inh.nTerm.Category, (int)token];
                }
                if (pp == null)
                {
                    MessageBox.Show(" syntax error !");
                    return(false);
                }
                if (sma != null)
                {
                    fret = sma.FireAction(Stack, pp, lexx.TokenValue); // copy inherited top down into next production fields
                    if (fret == false)
                    {
                        return(fret);
                    }
                }
                Stack.Pop();

                for (int j = pp.rhs.Count - 1; j >= 0; j--)
                {
                    Terminal tt = pp.rhs[j] as Terminal;
                    if (tt != null && tt.Token == TokenLookup.TokenKind.Lambda)
                    {
                        continue;
                    }
                    if (pp.synth != null && pp.synth[j] != null)
                    {
                        if (pp.synth[j] is SDeclTypeAttribute)
                        {
                            SDeclTypeAttribute sa = (SDeclTypeAttribute)pp.synth[j] as SDeclTypeAttribute;
                            Stack.Push(new SDeclTypeAttribute(sa));
                        }
                        pp.synth[j].Clear();
                    }

                    if (tt != null)
                    {
                        Stack.Push(pp.rhs[j]);
                    }

                    if (pp.inh != null && pp.inh[j] != null)
                    {
                        if (pp.inh[j] is IDeclTypeAttribute)
                        {
                            IDeclTypeAttribute ie = pp.inh[j] as IDeclTypeAttribute;
                            Stack.Push(new IDeclTypeAttribute(ie));
                        }
                        else
                        {
                            Stack.Push(new InheritedAttribute(pp.inh[j]));
                        }
                        pp.inh[j].Clear();
                    }
                    //}

                    if (pp.action != null && pp.action[j] != null)
                    {
                        Stack.Push(pp.action[j]);
                    }
                }
                //DumpStack();
            }
            else // is  a Terminal or EOI
            {
                Terminal tt = sym as Terminal;
                if (token == TokenLookup.TokenKind.EOI)
                {
                    _curline = 1;
                    MessageBox.Show("stream successfully parsed ");
                    return(false);
                }
                else
                if (token == tt.Token)
                {
                    if (sma != null)
                    {
                        fret = sma.FireAction(Stack, null, lexx.TokenValue);

                        if (fret == false)
                        {
                            return(fret);
                        }
                    }
                    Stack.Pop();
                    token = lexx.getToken();
                    return(true);
                }
                else
                {
                    MessageBox.Show("Unexpected token : " + token + ", expected : " + tt.Token);
                    return(false);
                }
            }
            return(true);
        }