Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Code for parsing .c file in java

  1. #1
    Junior Member Kakashi's Avatar
    Join Date
    Oct 2009
    Posts
    29
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Code for parsing .c file in java

    Hi, so I am working on geting this code to parse a .c file I am using a enum for defining the token.
    enum Token {NOT_FOUND, UPPER_CASE_IDENTIFIER, CONSTANT, COMMENT,
    COMPILER_DIRECTIVE, ASSIGNMENT_OPERATOR,
    PRE_OR_POST_UNARY_OPERATOR, STRUCTURE_OPERATOR,
    UNARY_OR_BINARY_OPERATOR,UNARY_OPERATOR, BINARY_OPERATOR,
    TERNARY_OPERATOR, COLON, LEFT_PARENTHESIS, RIGHT_PARENTHESIS,
    LEFT_BRACKET, RIGHT_BRACKET, LEFT_BRACE, RIGHT_BRACE, SEMICOLON,
    COMMA, STRING, SC_SPECIFIER, BREAK, CASE, TYPE_SPECIFIER,
    CONTINUE, DEFAULT, DO, ELSE, FOR, GOTO, IF, RETURN, SIZEOF,
    STATUS, STRUCT, SWITCH, UNION, WHILE, IDENTIFIER, FIRST, NONE,
    END_OF_FILE}

    this is my main code
    import java.io.*;
    class Main
    {
     
        //  The main function for the C formatter program.  It creates
        //     the three primary objects, an output object, a lexer object,
        //     and a formatter object. It then calls the file method of the
        //     formatter object to perform the formatting.
     
        private static final BufferedReader stdin =
            new BufferedReader(new InputStreamReader(System.in));
     
        public static void main(String[] args) throws IOException
        {
            String fileName;
            System.out.print("Enter file name without .c: ");
            fileName = stdin.readLine();
            Output output = new Output(fileName);
            Lexer lexer = new Lexer(fileName, output);
            Format format = new Format(lexer, output);
     
            format.file();
            lexer.close();
            output.close();
        }
    }
    What is the matter is I cant figure out how to parse the if and while loops.
    here is the formater class that I made
    class Format
    {
        private Lexer lexer;
        private Output output;
        private Token token;
     
        //  The constructor establishes the input lexer and the output
        //    private data members.
     
        public Format(Lexer lexer, Output output)
        {
            this.lexer = lexer;
            this.output = output;
        }
     
        //  file is the only public method. External
        //    declarations are formatted until a function is found, at 
        //    which time functionBody is called to format it.
     
        public void file()
        {
            token = lexer.getNextToken();
            while (token != Token.END_OF_FILE)
                if (externalDeclaration())
                    functionBody();
        }
     
        //  functionBody formats the declarations and statements in a
        //    function body.
     
        private void functionBody()
        {
            output.endLine(true);
            while (token == Token.TYPE_SPECIFIER ||
                token == Token.SC_SPECIFIER ||
                token == Token.STRUCT || token == Token.UNION ||
                token == Token.UPPER_CASE_IDENTIFIER)
                parameterDeclaration();
            output.indent();
            output.endLine(false);
            output.skipLine();
            compoundStatement();
            output.unindent();
            output.endLine(false);
            output.endPage();
        }
     
        // compoundStatement formats a multiple statement block
     
        private void compoundStatement()
        {
            int noOfDeclarations= 0;
            token = lexer.getNextToken();
            output.endLine(false);
            while (token == Token.TYPE_SPECIFIER ||
                token == Token.SC_SPECIFIER ||
                token == Token.STRUCT || token == Token.UNION ||
                token == Token.UPPER_CASE_IDENTIFIER)
            {
                declaration();
                noOfDeclarations++;
            }
            if (noOfDeclarations > 0)
                output.skipLine();
            while (token != Token.RIGHT_BRACE)
                statement();
            token = lexer.getNextToken();
            output.endLine(false);
        }
     
        //  statement determines the type of statement and calls the
        //    appropriate function to format it.
     
        private void statement()
        {
            if (token == Token.IDENTIFIER)
                if ((token = lexer.getNextToken()) == Token.COLON)
                    token = lexer.getNextToken();
                else
                {
                    token = Token.IDENTIFIER;
                    lexer.putLastToken();
                }
            switch (token)
            {
                case LEFT_BRACE:
                    compoundStatement();
                    break;
                case SWITCH:
                    switchStatement();
                    break;
                case BREAK:
                case CONTINUE:
                    verifyNextToken(Token.SEMICOLON);
                    output.endLine(false);
                   break;
                case RETURN:
                    if ((token = lexer.getNextToken()) != Token.SEMICOLON)
                        {
                        expression(Token.SEMICOLON);
                        token = lexer.getNextToken();
                        }
                    else
                        token = lexer.getNextToken();
                    output.endLine(false);
                    break;
                case GOTO:
                    verifyNextToken(Token.IDENTIFIER);
                    verifyNextToken(Token.SEMICOLON);
                    output.endLine(false);
                    break;
                default:
                    expression(Token.SEMICOLON);
                    token = lexer.getNextToken();
                    output.endLine(false);
                }
            }
     
         //  switchStatement formats a switch statement.
     
        private void switchStatement()
        {
            verifyNextToken(Token.LEFT_PARENTHESIS);
            expression(Token.RIGHT_PARENTHESIS);
            token = lexer.getNextToken();
            output.endLine(false);
            output.indent();
            verifyCurrentToken(Token.LEFT_BRACE);
            output.endLine(false);
            while (token == Token.CASE || token == Token.DEFAULT)
            {
                if (token == Token.CASE)
                {
                    expression(Token.COLON);
                    lexer.adjustSpacing(Lexer.SUPPRESS_LEADING_SPACE);
                    token = lexer.getNextToken();
                    output.endLine(false);
                    output.indent();
                    while (token != Token.CASE && token !=
                        Token.DEFAULT && token != Token.RIGHT_BRACE)
                    statement();
                    output.unindent();
                }
                else
                {
                    expression(Token.COLON);
                    lexer.adjustSpacing(Lexer.SUPPRESS_LEADING_SPACE);
                    token = lexer.getNextToken();
                    output.endLine(false);
                    output.indent();
                    while (token != Token.CASE && token != Token.DEFAULT &&
                        token != Token.RIGHT_BRACE)
                        statement();
                    output.unindent();
                }
            }
            verifyCurrentToken(Token.RIGHT_BRACE);
            output.endLine(false);
            output.unindent();
        }
     
        // externalDeclarations formats external declarations such as
        //   global variables and function prototypes. It returns if
        //   it encounters a function heading.
     
        private boolean externalDeclaration()
     
        {
            int braceCount = 0;
            boolean indentAtSemicolon = false;
            Token lastToken = Token.NOT_FOUND;
            while ((braceCount > 0) || (token != Token.SEMICOLON))
            {
                lexer.checkDeclarationSpacing(token);
                if (token == Token.LEFT_BRACE)
                {
                    output.endLine(false);
                    output.indent();
                    lastToken = token;
                    token = lexer.getNextToken();
                    output.endLine(false);
                    braceCount++;
                }
                else if (token == Token.RIGHT_BRACE)
                {
                    lastToken = token;
                    token = lexer.getNextToken();
                    indentAtSemicolon = true;
                    braceCount--;
                }
                else if (token == Token.LEFT_PARENTHESIS)
                {
                    lastToken = token;
                    token = lexer.getNextToken();
                }
                else if (token == Token.RIGHT_PARENTHESIS)
                {
                    lastToken = token;
                    token = lexer.getNextToken();
                    if (token != Token.SEMICOLON)
                        return true;
                }
                else if (token == Token.ASSIGNMENT_OPERATOR)
                    while (token != Token.SEMICOLON)
                    {
                        lastToken = token;
                        token = lexer.getNextToken();
                        lexer.checkExpressionSpacing(token, lastToken);
                    }
                else if (token == Token.SEMICOLON)
                {
                    lastToken = token;
                    token = lexer.getNextToken();
                    if (braceCount > 0)
                        output.endLine(false);
                    if (indentAtSemicolon)
                    {
                        output.indent();
                        indentAtSemicolon = false;
                    }
                }
                else
                {
                    lastToken = token;
                    token = lexer.getNextToken();
                }
            }
     
            token = lexer.getNextToken();
            output.endLine(false);
            if (indentAtSemicolon)
                output.indent();
            return false;
        }
     
        //  parameterDeclaration formats parameter declarations.
     
        private void parameterDeclaration()
     
        {
            int braceCount = 0;
            while ((braceCount > 0) || (token != Token.SEMICOLON))
            {
                lexer.checkDeclarationSpacing(token);
                if (token == Token.LEFT_BRACE)
                {
                    output.endLine(false);
                    output.indent();
                    token = lexer.getNextToken();
                    output.endLine(false);
                    braceCount++;
                }
            else if (token == Token.RIGHT_BRACE)
                {
                    token = lexer.getNextToken();
                    output.indent();
                    braceCount--;
                }
            else if ((braceCount > 0 ) && (token == Token.SEMICOLON))
                {
                    token = lexer.getNextToken();
                    output.endLine(false);
                }
            else
                token = lexer.getNextToken();
            }
            token = lexer.getNextToken();
            output.endLine(false);
        }
     
        // declaration formats local declarations.
     
        private void declaration()
     
        {
            int braceCount = 0;
            boolean indentAtSemicolon = false;
     
            while ((braceCount > 0) || (token != Token.SEMICOLON))
            {
                lexer.checkDeclarationSpacing(token);
                if (token == Token.LEFT_BRACE)
                {
                    output.endLine(false);
                    output.indent();
                    token = lexer.getNextToken();
                    output.endLine(false);
                    braceCount++;
                }
                else if (token == Token.RIGHT_BRACE)
                {
                    token = lexer.getNextToken();
                    indentAtSemicolon = true;
                    braceCount--;
                }
                else if (token == Token.SEMICOLON)
                {
                    token = lexer.getNextToken();
                    if (braceCount > 0)
                        output.endLine(false);
                    if (indentAtSemicolon)
                    {
                        output.indent();
                        indentAtSemicolon = false;
                    }
                }
                else if (token == Token.ASSIGNMENT_OPERATOR)
                    expression(Token.SEMICOLON);
                else
                    token = lexer.getNextToken();
            }
            token = lexer.getNextToken();
            output.endLine(false);
            if (indentAtSemicolon)
                output.indent();
        }
     
        //  expression formats an expression. The delimiting token must
        //    be provided.
     
        private void expression(Token terminator)
        {
            Token lastToken;
     
            lastToken = Token.NOT_FOUND;
            while (token != terminator)
            {
                lexer.checkExpressionSpacing(token, lastToken);
     
                if (token == Token.LEFT_PARENTHESIS)
                {
                    if (lastToken == Token.IDENTIFIER ||
                        lastToken == Token.UPPER_CASE_IDENTIFIER)
                        lexer.adjustSpacing(Lexer.SUPPRESS_LEADING_SPACE);
                    token = lexer.getNextToken();
                    expression(Token.RIGHT_PARENTHESIS);
                }
                lastToken = token;
                token = lexer.getNextToken();
            }
        }
     
        // Gets the next token and then verifies that the supplied token is
        //   the required token.
     
        private void verifyNextToken(Token requiredToken)
        {
            token = lexer.getNextToken();
            verifyCurrentToken(requiredToken);
        }
     
        // Verifies that the supplied token is the current token.
        // Displays an error message if it is not.
     
        private void verifyCurrentToken(Token requiredToken)
        {
            if (token != requiredToken)
                output.outputError("MISSING " + requiredToken.name());
            else
                token = lexer.getNextToken();
     
        }
    }
    Thanks


  2. #2
    Junior Member Kakashi's Avatar
    Join Date
    Oct 2009
    Posts
    29
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Code for parsing .c file in java

    Ok so I got the for loop to work and now I started to while loop but when I run the code it produces an output that is too big for notepad to open here is the for loop code
    //  forStatement formats a for statement.
        private void forStatement() {
        	verifyNextToken(Token.LEFT_PARENTHESIS);
            expression(Token.RIGHT_PARENTHESIS);
            token = lexer.getNextToken();
            output.endLine(false);
            output.indent();
            verifyCurrentToken(Token.LEFT_BRACE);
            output.endLine(false);
            expression(Token.SEMICOLON);
            lexer.adjustSpacing(Lexer.SUPPRESS_LEADING_SPACE);
            token = lexer.getNextToken();
            output.endLine(false);
            output.unindent();
            verifyCurrentToken(Token.RIGHT_BRACE);
            output.endLine(false);
            output.unindent();
        }
    And the while loop code looks the same, i cant figure out what is wrong with the while loop code.
    //  whileStatement formats a while statement.
        private void whileStatemnt() {
        	verifyNextToken(Token.LEFT_PARENTHESIS);
            expression(Token.RIGHT_PARENTHESIS);
            token = lexer.getNextToken();
            output.endLine(false);
            output.indent();
            verifyCurrentToken(Token.LEFT_BRACE);
            output.endLine(false);
            expression(Token.SEMICOLON);
            lexer.adjustSpacing(Lexer.SUPPRESS_LEADING_SPACE);
            token = lexer.getNextToken();
            output.endLine(false);
            output.unindent();
            verifyCurrentToken(Token.RIGHT_BRACE);
            output.endLine(false);
            output.unindent();
        }

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Code for parsing .c file in java

    Quote Originally Posted by Kakashi View Post
    Ok so I got the for loop to work and now I started to while loop but when I run the code it produces an output that is too big for notepad to open here is the for loop code
    //  forStatement formats a for statement.
        private void forStatement() {
        	verifyNextToken(Token.LEFT_PARENTHESIS);
            expression(Token.RIGHT_PARENTHESIS);
            token = lexer.getNextToken();
            output.endLine(false);
            output.indent();
            verifyCurrentToken(Token.LEFT_BRACE);
            output.endLine(false);
            expression(Token.SEMICOLON);
            lexer.adjustSpacing(Lexer.SUPPRESS_LEADING_SPACE);
            token = lexer.getNextToken();
            output.endLine(false);
            output.unindent();
            verifyCurrentToken(Token.RIGHT_BRACE);
            output.endLine(false);
            output.unindent();
        }
    And the while loop code looks the same, i cant figure out what is wrong with the while loop code.
    //  whileStatement formats a while statement.
        private void whileStatemnt() {
        	verifyNextToken(Token.LEFT_PARENTHESIS);
            expression(Token.RIGHT_PARENTHESIS);
            token = lexer.getNextToken();
            output.endLine(false);
            output.indent();
            verifyCurrentToken(Token.LEFT_BRACE);
            output.endLine(false);
            expression(Token.SEMICOLON);
            lexer.adjustSpacing(Lexer.SUPPRESS_LEADING_SPACE);
            token = lexer.getNextToken();
            output.endLine(false);
            output.unindent();
            verifyCurrentToken(Token.RIGHT_BRACE);
            output.endLine(false);
            output.unindent();
        }
    is this working code or not, wha type of output of this code

    --- Update ---

    hii
    is this code working or not

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Code for parsing .c file in java

    This thread is over 3 years old -- please don't re-animate it. If you have a question to ask in the forum, please feel free to start a new thread. Locking.

Similar Threads

  1. Java EE warnings while parsing wsdl
    By Idy in forum Web Frameworks
    Replies: 0
    Last Post: January 14th, 2010, 04:29 PM
  2. java xml-rpc response parsing to xml
    By kievari in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: November 19th, 2009, 02:36 PM
  3. Input file parsing to insert into DB
    By IDForums in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: September 30th, 2009, 02:29 AM
  4. Printing xml to the console from .wmdb without printing junks
    By John in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: April 24th, 2009, 03:44 AM
  5. [SOLVED] Parsing ID3 tags from mp3
    By John in forum Java Theory & Questions
    Replies: 14
    Last Post: April 16th, 2009, 01:36 PM