Results: How to become good coder? 

Voters
1 You may not vote on this poll

  • What are some of the secrets thatmake one to be come gig?
    0%
  • Can any one help me with nice stuff on compiler interpreter & design?
    100.00%
  • What is the marketable cs are of field studies?
    0%
  • Can any one know how hard to become programmer with lemited HR?
    0%
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0

    Compiler Interpreter & Design( Modify the print statement to out put string constant)


    Hello,
    Extend mcc with the data types float, double and boolean. Modify the type
    checking accordingly. Modify the print statement, such that it can output a string constant.
    Implement the extensions for the compiler and the interpreter.
    Here is my grammars; nakedParser.jj and parser.jj classes for parser generator. Yes i did implement my typeChecking and the semantic classes but the files are showing this error:Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The method parseBool(String) is undefined for the type Boolean
    Below are my to parser classes for the grammar;
    public class Parser {

    private static Message m = new Message("parser");
    }

    PARSER_END(Parser)

    /* WHITE SPACE */

    SKIP :
    {
    " " | "\t" | "\n" | "\r" | "\f"
    }

    /* COMMENTS */

    SKIP :
    {
    "//" : IN_SINGLE_LINE_COMMENT
    |
    "/*" : IN_MULTI_LINE_COMMENT
    }

    <IN_SINGLE_LINE_COMMENT> SKIP :
    {
    < "\n" | "\r" | "\r\n" > : DEFAULT
    }

    <IN_MULTI_LINE_COMMENT> SKIP :
    {
    < "*/" > : DEFAULT
    }

    <IN_SINGLE_LINE_COMMENT,IN_MULTI_LINE_COMMENT> SKIP :
    {
    < ~[] >
    }

    /* RESERVED WORDS AND LITERALS */

    TOKEN :
    {
    < ELSE: "else" >
    | < IF: "if" >
    | < INT: "int" >
    | < DOUBLE: "double" >
    | < FLOAT: "float" >
    | < BOOLEAN: "boolean" >
    | < WHILE: "while" >
    | < RETURN: "return" >
    | < PRINT: "print" >
    }

    TOKEN :
    {
    < LP: "(" >
    | < RP: ")" >
    | < LB: "{" >
    | < RB: "}" >
    | < SE: ";" >
    | < CO: "," >
    }

    /* OPERATORS */

    TOKEN :
    {
    < ASSIGN: "=" >
    | < GT: ">" >
    | < LT: "<" >
    | < EQ: "==" >
    | < LE: "<=" >
    | < GE: ">=" >
    | < NE: "!=" >
    | < ADD: "+" >
    | < SUB: "-" >
    | < MUL: "*" >
    | < DIV: "/" >
    | < OR: "||" >
    | < AND: "&&" >
    | < NOT: "!" >
    }

    /* LITERALS */

    TOKEN :
    {
    < INTEGER_CST: ["0"-"9"] (["0"-"9"])* >
    |
    < FLOAT_CST:[0-9]+ .([0-9]* | .[0-9]+) >
    |
    < BOOLEAN_CST:"true" | "false">
    }

    /* IDENTIFIERS */

    TOKEN :
    {
    < IDENTIFIER: <LETTER> (<LETTER>|<DIGIT>)* >
    |
    < #LETTER: ["a"-"z","A"-"Z","_"]>
    |
    < #DIGIT: [ "0"-"9"] >
    }


    ASTProgram program() :
    {
    ASTProgram p = new ASTProgram();
    ASTDeclaration d = null;
    ASTStatement s = null;
    }
    {
    {
    m.verbose("> program");
    m.i();
    }
    (
    LOOKAHEAD(3) d = variableDeclaration()
    {
    p.addDeclaration(d);
    }
    )*
    (
    LOOKAHEAD(3) d = functionDeclaration()
    {
    p.addDeclaration(d);
    }
    )*
    (
    s = statement()
    {
    p.addStatement(s);
    }
    )*
    <EOF>
    {
    m.d();
    m.verbose("< program");
    return p;
    }
    }

    ASTVarDeclaration variableDeclaration() :
    {
    Token t = null;
    }
    {
    {
    m.verbose("> variable declaration");
    m.i();
    }
    //int
    <INT_CST> t = <IDENTIFIER> <SE>
    {
    m.d();
    m.verbose("< variable declaration");
    return new ASTVarDeclaration(t.image, "int");
    }
    //double
    < DOUBLE_CST > t =< IDENTIFIER > <SE >
    {
    m.d();
    m.verbose("< variable declaration");
    return new ASTVarDeclaration(t.image, "double");
    }
    //float
    < FLOAT_CST > t =< IDENTIFIER > <SE >
    {
    m.d();
    m.verbose("< variable declaration");
    return new ASTVarDeclaration(t.image, "float");
    }
    //boolean
    < BOOLEAN_CST > t =< IDENTIFIER > <SE >
    {
    m.d();
    m.verbose("< variable declaration");
    return new ASTVarDeclaration(t.image, "boolean");
    }

    }

    ASTDeclaration functionDeclaration() :
    {
    Token t = null;
    ASTFunDeclaration d = null;
    ASTBlockStatement b = null;
    }
    {
    {
    m.verbose("> function declaration");
    m.i();
    }
    (<INT_CST>|< DOUBLE_CST >|< FLOAT_CST >|< BOOLEAN_CST >) t = <IDENTIFIER>
    {
    d = new ASTFunDeclaration(t.image, "int");
    d = new ASTFunDeclaration(t.image, "double");
    d = new ASTFunDeclaration(t.image, "float");
    d = new ASTFunDeclaration(t.image, "boolean");
    }
    <LP> parameterDeclarations(d) <RP> b = blockStatement()
    {
    d.block = b;
    m.d();
    m.verbose("< function declaration");
    return d;
    }
    }

    void parameterDeclarations(ASTFunDeclaration d) :
    {
    ASTParDeclaration p = null;
    Token t = null;
    }
    {
    {
    m.verbose("> parameter declaration");
    m.i();
    }
    //own code
    (
    (<INT_CST>|< DOUBLE_CST >|< FLOAT_CST >|< BOOLEAN_CST >) t = <IDENTIFIER>
    {
    p = new ASTParDeclaration(t.image, "int");
    d.addDeclaration(p);
    p = new ASTParDeclaration(t.image, "double");
    d.addDeclaration(p);
    p = new ASTParDeclaration(t.image, "float");
    d.addDeclaration(p);
    p = new ASTParDeclaration(t.image, "boolean");
    d.addDeclaration(p);
    }
    (
    <CO> <INT_CST> t = <IDENTIFIER>
    {
    p = new ASTParDeclaration(t.image, "int");
    d.addDeclaration(p);
    }
    //adjusted
    <CO> <FLOAT_CST>
    {
    p = new ASTParDeclaration(t.image, "float");
    d.addDeclaration(p);
    }
    //adjusted
    <CO> <BOOLEAN_CST>
    {
    p = new ASTParDeclaration(t.image, "boolean");
    d.addDeclaration(p);
    }
    )*
    )?
    {
    m.d();
    m.verbose("< parameter declaration");
    }
    }

    ASTBlockStatement blockStatement() :
    {
    ASTBlockStatement b = new ASTBlockStatement();
    ASTVarDeclaration v = null;
    ASTStatement s = null;
    Token t;
    }
    {
    {
    m.verbose("> block");
    m.i();
    }
    t = <LB>
    (
    v = variableDeclaration()
    {
    b.addDeclaration(v);
    }
    )*
    (
    s = statement()
    {
    b.addStatement(s);
    }
    )*
    <RB>
    {
    b.setPosition(t);
    m.d();
    m.verbose("< block");
    return b;
    }
    }

    ASTStatement statement() :
    {
    ASTStatement s = null;
    ASTExpression e = null;
    Token t = null;
    Token u = null;
    }
    {
    {
    m.verbose("> statement");
    m.i();
    }
    (
    {
    m.verbose("> assign statement");
    m.i();
    }
    t = <IDENTIFIER> u = <ASSIGN> e = addExpression() <SE>
    {
    ASTVariable v = new ASTVariable(t.image);
    v.setPosition(t);
    s = new ASTAssignStatement(v, e);
    s.setPosition(u);
    m.d();
    m.verbose("< assign statement");
    }
    |
    {
    m.verbose("> while statement");
    m.i();
    }
    s = whileStatement()
    {
    m.d();
    m.verbose("< while statement");
    }
    |
    {
    m.verbose("> if statement");
    m.i();
    }
    s = ifStatement()
    {
    m.d();
    m.verbose("< if statement");
    }
    |
    {
    m.verbose("> return statement");
    m.i();
    }
    u = <RETURN> e = addExpression() <SE>
    {
    s = new ASTReturnStatement(e);
    s.setPosition(u);
    m.d();
    m.verbose("< return statement");
    }
    |
    {
    m.verbose("> print statement");
    m.i();
    }
    <PRINT> <LP> e = addExpression() <RP> <SE>
    {
    s = new ASTPrintStatement(e);
    m.d();
    m.verbose("< print statement");
    }
    |
    s = blockStatement()
    )
    {
    m.d();
    m.verbose("< statement");
    return s;
    }
    }

    ASTWhileStatement whileStatement() :
    {
    ASTExpression e = null;
    ASTStatement s = null;
    }
    {
    <WHILE> <LP> e = orExpression() <RP>
    s = statement()
    {
    return new ASTWhileStatement(e, s);
    }
    }

    ASTIfStatement ifStatement() :
    {
    ASTExpression e = null;
    ASTStatement st = null;
    ASTStatement se = null;
    }
    {
    <IF> <LP> e = orExpression() <RP>
    st = statement()
    (
    <ELSE> se = statement()
    {
    return new ASTIfStatement(e, st, se);
    }
    )?
    {
    return new ASTIfStatement(e, st);
    }
    }

    ASTExpression orExpression() :
    {
    ASTExpression result = null;
    ASTExpression e = null;
    Token t;
    }
    {
    {
    m.verbose("> or expression");
    m.i();
    }
    result = andExpression()
    (
    t = <OR> e = andExpression()
    {
    result = new ASTOr(result, e);
    result.setPosition(t);
    }
    )*
    {
    m.d();
    m.verbose("< or expression");
    return result;
    }
    }

    ASTExpression andExpression() :
    {
    ASTExpression result = null;
    ASTExpression e = null;
    Token t;
    }
    {
    {
    m.verbose("> and expression");
    m.i();
    }
    result = relExpression()
    (
    t = <AND> e = relExpression()
    {
    result = new ASTAnd(result, e);
    result.setPosition(t);
    }
    )*
    {
    m.d();
    m.verbose("< and expression");
    return result;
    }
    }

    ASTExpression relExpression() :
    {
    ASTExpression result = null;
    ASTExpression e = null;
    Token t = null;
    }
    {
    {
    m.verbose("> relation expression");
    m.i();
    }
    result = addExpression()
    (
    t = <GT> e = addExpression()
    {
    result = new ASTGt(result,e);
    result.setPosition(t);
    }
    |
    t = <GE> e = addExpression()
    {
    result = new ASTGe(result,e);
    result.setPosition(t);
    }
    |
    t = <EQ> e = addExpression()
    {
    result = new ASTEq(result,e);
    result.setPosition(t);
    }
    |
    t = <NE> e = addExpression()
    {
    result = new ASTNe(result,e);
    result.setPosition(t);
    }
    |
    t = <LT> e = addExpression()
    {
    result = new ASTLt(result,e);
    result.setPosition(t);
    }
    |
    t = <LE> e = addExpression()
    {
    result = new ASTLe(result,e);
    result.setPosition(t);
    }
    )?
    {
    m.d();
    m.verbose("< relation expression");
    return result;
    }
    }

    ASTExpression addExpression() :
    {
    ASTExpression result = null;
    ASTExpression e = null;
    Token t;
    }
    {
    {
    m.verbose("> add expression");
    m.i();
    }
    result = mulExpression()
    (
    t = <ADD> e = mulExpression()
    {
    result = new ASTAdd(result, e);
    result.setPosition(t);
    }
    |
    t = <SUB> e = mulExpression()
    {
    result = new ASTSub(result, e);
    result.setPosition(t);
    }
    )*
    {
    m.d();
    m.verbose("< add expression");
    return result;
    }
    }

    ASTExpression mulExpression() :
    {
    ASTExpression result = null;
    ASTExpression e = null;
    Token t;
    }
    {
    {
    m.verbose("> mul expression");
    m.i();
    }
    result = negExpression()
    (
    t = <MUL> e = negExpression()
    {
    result = new ASTMul(result, e);
    result.setPosition(t);
    }
    |
    t = <DIV> e = negExpression()
    {
    result = new ASTDiv(result, e);
    result.setPosition(t);
    }
    )*
    {
    m.d();
    m.verbose("< mul expression");
    return result;
    }
    }

    ASTExpression negExpression() :
    {
    ASTExpression result = null;
    ASTExpression e = null;
    Token t;
    }
    {
    {
    m.verbose("> neg expression");
    m.i();
    }
    (
    t = <SUB> e = priExpression()
    {
    result = new ASTNeg(e);
    result.setPosition(t);
    }
    |
    t = <NOT> e = priExpression()
    {
    result = new ASTNot(e);
    result.setPosition(t);
    }
    |
    result = priExpression()
    )
    {
    m.d();
    m.verbose("< neg expression");
    return result;
    }
    }

    ASTExpression priExpression() :
    {
    ASTExpression result = null;
    ASTCall c = null;
    Token t = null;
    }
    {
    {
    m.verbose("> pri expression");
    m.i();
    }
    (
    t = <INTEGER_CST>
    {
    result = new ASTIntegerConstant(Integer.parseInt(token.image));
    result.setPosition(t);
    }
    |
    //ADJUSTED
    t = < DOUBLE_CST >
    {
    result = new ASTDoubleConstant(Double.parseDouble(token.image));
    result.setPosition(t);
    }
    |
    t = < FLOAT_CST >
    {
    result = new ASTFloatConstant(Float.parseFloat(token.image));
    result.setPosition(t);
    }
    |
    t = < BOOLEAN_CST >
    {
    result = new ASTBooleanConstant(Boolean.parseBoolean(token.image));
    result.setPosition(t);
    }
    //END
    |
    <LP> result = orExpression() <RP>
    |
    t = <IDENTIFIER>
    {
    result = new ASTVariable(t.image);
    result.setPosition(t);
    }
    (
    <LP>
    {
    c = new ASTCall(t.image);
    result = c;
    }
    (arguments(c))?

    <RP>
    {
    result.setPosition(t);
    }
    )?
    )
    {
    m.d();
    m.verbose("< pri expression");
    return result;
    }
    }

    void arguments(ASTCall f) :
    {
    ASTExpression e = null;
    }
    {
    e = addExpression()
    {
    f.addParameter(e);
    }
    (
    <CO> e = addExpression()
    {
    f.addParameter(e);
    }
    )*
    }

    for the naked parser class;

    /**
    * mc is a small subset of c
    * the grammar of mc is defined as follows:
    *
    * program ::= (variableDeclaration)*
    * (functionDeclaration)*
    * (statement)*
    * ;
    *
    * variableDeclaration ::= "int" <ID> ";"
    * ;
    *
    * functionDeclaration ::= "int" <ID> "(" funcParam ")" blockStatement
    * ;
    *
    * parameterDeclaration ::= ("int" <ID> ("," "int" <ID>)*)?
    * ;
    *
    * blockStatement ::= "{" (varDeclaration)* (statement)* "}"
    * ;
    *
    * statement ::= <ID> "=" addExpression ";"
    * |
    * whileStatement
    * |
    * ifStatement
    * |
    * "return" addExpression ";"
    * |
    * "print" "(" addExpression ")" ";"
    * |
    * blockStatement
    * ;
    *
    * whileStatement ::= "while" "(" orExpression ")" statement
    * ;
    *
    * ifStatement ::= "if" "(" orExpression ")" statement
    * ("else" statement)?
    * ;
    *
    * orExpression ::= andExpression ("||" andExpression)*
    * ;
    *
    * andExpression ::= relExpression ("&&" relExpression)*
    * ;
    *
    * relExpression ::= addExpression
    * (
    * (">" | ">=" | "==" | "!=" | "<" | "<=")
    * addExpression
    * )?
    * ;
    *
    * addExpression ::= mulExpression
    * (("+" | "-") mulExpression)*
    * ;
    *
    * mulExpression ::= negExpression
    * (("*" | "/") negExpression)*
    * ;
    *
    * negExpression ::= ("-" | "!")? priExpression
    * ;
    *
    * priExpression ::= <INTEGER>
    * |
    * "(" orExpression ")"
    * |
    * <ID> ("(" (arguments)? ")")?
    * ;
    *
    * arguments ::= orExpression ("," orExpression)*
    * ;
    */

    PARSER_BEGIN(NakedParser)

    package utg.mcc.parser;

    public class NakedParser {}

    PARSER_END(NakedParser)

    /* WHITE SPACE */

    SKIP :
    {
    " " | "\t" | "\n" | "\r" | "\f"
    }

    /* COMMENTS */

    SKIP :
    {
    "//" : IN_SINGLE_LINE_COMMENT
    |
    "/*" : IN_MULTI_LINE_COMMENT
    }

    <IN_SINGLE_LINE_COMMENT> SKIP :
    {
    < "\n" | "\r" | "\r\n" > : DEFAULT
    }

    <IN_MULTI_LINE_COMMENT> SKIP :
    {
    < "*/" > : DEFAULT
    }

    <IN_SINGLE_LINE_COMMENT,IN_MULTI_LINE_COMMENT> SKIP :
    {
    < ~[] >
    }

    /* RESERVED WORDS AND LITERALS */

    TOKEN :
    {

    < ELSE: "else" >
    | < IF: "if" >
    | < INT: "int" >
    | < DOUBLE: "double" >
    | < FLOAT: "float" >
    | < BOOLEAN: "boolean" >
    | < WHILE: "while" >
    | < RETURN: "return" >
    | < PRINT: "print" >
    }

    /* LITERALS */

    TOKEN :
    {
    < INTEGER_CST: ["0"-"9"] (["0"-"9"])* >
    |
    < FLOAT_CST:[0-9]+ .([0-9]* | .[0-9]+) >
    |
    < BOOLEAN_CST:"True" | "False" >
    }

    /* IDENTIFIERS */

    TOKEN :
    {
    < IDENTIFIER: <LETTER> (<LETTER>|<DIGIT>)* >
    |
    < #LETTER: ["a"-"z","A"-"Z","_"]>
    |
    < #DIGIT: [ "0"-"9"] >
    }


    void program() :
    {}
    {
    (LOOKAHEAD(3) variableDeclaration())*
    (LOOKAHEAD(3) functionDeclaration())*
    (statement())*
    }

    void variableDeclaration() :
    {}
    {
    ( "int"| "double"|"float"|"boolean")< IDENTIFIER > ";"

    }
    void functionDeclaration() :
    {}
    {
    ("int"| "double"|"float"|"boolean") <IDENTIFIER> "(" parameterDeclarations() ")" blockStatement()

    }

    void parameterDeclarations() :
    {}
    {
    ("int" <IDENTIFIER> ("," "int" <IDENTIFIER>)*)?
    ("double" <IDENTIFIER> ("," "double" <IDENTIFIER>)*)?
    ("float" <IDENTIFIER> ("," "float" <IDENTIFIER>)*)?
    ("boolean" <IDENTIFIER> ("," "boolean" <IDENTIFIER>)*)?
    }

    void blockStatement() :
    {}
    {
    "{" (variableDeclaration())* (statement())* "}"
    }

    void statement() :
    {}
    {
    <IDENTIFIER> "=" addExpression() ";"
    |
    whileStatement()
    |
    ifStatement()
    |
    "return" addExpression() ";"
    |
    //MY CODE

    "print" "(" addExpression() ")" ";"
    |
    blockStatement()
    }

    void whileStatement() :
    {}
    {
    "while" "(" orExpression() ")"
    statement()
    }

    void ifStatement() :
    {}
    {
    "if" "(" orExpression() ")"
    statement()
    ("else" statement())?
    }

    void orExpression() :
    {}
    {
    andExpression() ("||" andExpression())*
    }

    void andExpression() :
    {}
    {
    relExpression() ("&&" relExpression())*
    }

    void relExpression() :
    {}
    {
    addExpression()
    ((">" | ">=" | "==" | "!=" | "<" | "<=")
    addExpression())?
    }

    void addExpression() :
    {}
    {
    mulExpression() (("+" | "-") mulExpression())*
    }

    void mulExpression() :
    {}
    {
    negExpression() (("*" | "/") negExpression())*
    }

    void negExpression() :
    {}
    {
    ("-" | "!")? priExpression()
    }

    void priExpression() :
    {}
    {
    <INTEGER> |< DOUBLE >|< FLOAT >|< BOOLEAN >
    |
    "(" orExpression() ")"
    |
    LOOKAHEAD(2) <IDENTIFIER>
    |
    LOOKAHEAD(2) <IDENTIFIER> "(" (arguments())? ")"
    }

    void arguments() :
    {}
    {
    addExpression() ("," addExpression())*
    }
    My doubt is how to correct the grammar to output any input as as a string constant. Please any one have idea help me with it. Also the code has show some errors that am uncapable to resoved it.
    This my project on Compiler Interpreter & Design using mean c compiler mcc
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    What language is that in? I cannot see where it follows C nor C++ nor even C# syntax. And the exception you post is for Java!

    Please post your message in the appropriate forum.

    Please format your code with proper indentation and use code tags to preserve that indentation! Posting an unreadable mess, which is what you've done, will result in nobody even trying to read it.

    And nobody wants to take your stupid poll. If you have a problem you need help on, then ask. If you want to conduct a poll, then create a thread specifically and solely for the purpose of conducting that poll.
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,074
    Rep Power
    1802
    Originally Posted by dwise1_aol
    What language is that in?
    Looks like JavaCC grammar definition for a C subset. (except in C "print" would be a library function, not part of the language itself.

    Either way Sedat would do well to actually ask a question rather than just dump his assignment.
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,074
    Rep Power
    1802
    I have no ideal how "voting" on the options in your poll will help you. The options should be possibles answers to a question, but you have posted questions.

    > What are some of the secrets that make one to be come gig?
    "become" is one word, and I have no idea what "gig" means in this context.

    > Can any one help me with nice stuff on compiler interpreter & design?
    You've posted a question on that, so maybe, but your question is badly formed. Help - yes, do it for you - no! As a general answer however you need Compilers: Principles, Techniques, and Tools.

    > What is the marketable cs are of field studies?
    Try again, I realise that English may not be your native language, but your translation is unintelligible.

    > Can any one know how hard to become programmer with lemited HR?
    "limited", and what is HR? What ever it is you are asking, the answer is probably yes, any one can know how hard it is.
    Last edited by clifford; December 11th, 2013 at 02:21 PM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0

    The wider the diameter of your knowledge the wider circumference of your ignorance!


    Yes I vividely agree with clifford, that I should ask rather than dumping some thing out of the scope, this is what make me realise that am not a perfect being.
    Just to scrolled back to my poll, guss what who cares I did accidential click on that worthless button, the dialog box samefully immenate its self without I know the reckless consequence behind it. Am sure perpetually sorry for all the injustic sorrow pain or psychological psyche pain I might course to any. I quote dwise1_aol," no body is interested on your stupid poll". Indeed it is but I believe if it wasn't that post I wouldn't learnt my mistake.
    Coming to clifford, those jargons like gig,HR, CS,become literally mean a gay sure much good in coding, human resources, computer science and "become" was sleep of the mind respectively.
    I appreciate your comment becouse every soul is entitile to your own opening and at all cost shall be respected! On that note I like critics but constructive and rationl manner.
    Good news, am already doing well with my project so I think this will serve as a tremendous motivation for me.

IMN logo majestic logo threadwatch logo seochat tools logo