http://kmlinux.fjfi.cvut.cz/~culikzde/sos/Lexer.zip

package example;
 
public class Arit {
    public String name;
    public Arit left;
    public Arit middle;
    public Arit right;
    public Arit (String name0) { name = name0; }
    public String toString () { return name; }
}
package example;
 
import static com.sun.corba.se.impl.util.Utility.printStackTrace;
 
public class Lexer {
 
    public String token;
 
    public static final int eos = 0; // end of source
    public static final int ident = 1;
    public static final int number = 2;
    public static final int separator = 3;
    public int kind;
 
    public Lexer (String input)
    {
        source = input;
 
        pos = 0;        
        len = source.length();
        line = 1;
        col = 0;
        nextChar ();      
        nextToken ();
    }
 
    private String source;
    private int pos;
    private int len;
    private char inpCh;
 
    private int line;
    private int col;
 
    private static final char zero = 0;
 
    private void nextChar ()
    {
        if (pos < len)
        {
           inpCh = source.charAt(pos);
           pos ++;
        }
        else
        {
            inpCh = zero;
        }
 
        if (inpCh == '\n') 
        {
            line ++;
            col = 0;
        }
        else
        {
            col ++;
        }
    }
 
    public void nextToken ()
    {
        while (isSpace (inpCh)) nextChar ();
 
        if (isLetter (inpCh))
        {
            kind = ident;
            token = "";
            while (isLetter(inpCh) || isDigit (inpCh))
            {
               token = token + inpCh;
               nextChar ();
            }
        }
        else if (isDigit (inpCh))
        {
            kind = number;
            token = "";
            while (isDigit (inpCh))
            {
               token = token + inpCh;
               nextChar ();
            }
        }
        else if (inpCh != zero)
        {
            kind = separator;
            token = "" + inpCh;
            nextChar ();
        }
        else
        {
            kind = eos;
            token = "";
        }
 
        String s = "line: " + line + 
                    ", column: " + col + 
                    ", inpCh: " + inpCh + 
                    ", token: " + token + 
                    ", kind: " + kind ;
        System.out.println (s);
    }
 
    private boolean isSpace (char c) 
    { 
        return c <= ' ' && c != zero;
    }
 
    private boolean isLetter (char c) 
    { 
        return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '_';
    }
 
    private boolean  isDigit (char c) 
    { 
        return c >= '0' && c <= '9';
    }
 
    /* ---- */
 
    public void error (String s)
    {
        s = s + ", line: " + line + 
                ", column: " + col + 
                ", inpCh: " + inpCh + 
                ", token: " + token + 
                ", kind: " + kind ;
        printStackTrace();
        throw new RuntimeException (s);
    }
 
    private final String quote = "\"";
 
    public boolean isSeparator (String s)
    {
        return kind == separator && token.equals (s);
    }
 
    public void checkSeparator (String s)
    {
        if (isSeparator(s))
            nextToken();
        else
            error (quote + s + quote + " expected");
    }
 
    public boolean isKeyword (String s)
    {
        return kind == ident && token.equals (s);
    }
 
    public void checkKeyword (String s)
    {
        if (isKeyword(s))
            nextToken();
        else
            error (quote + s + quote + " expected");
    }
}
package example;
 
public class Parser extends Lexer {
 
    public Parser (String input)
    {
        super (input);
    }
 
    public Arit factor ()
    {
        Arit result = null;
        if (isSeparator("("))
        {
           nextToken (); // preskocit (
           result = expr ();
           checkSeparator (")");
        }
        else if (kind == ident || kind == number)
        {
           result = new Arit (token);
           nextToken ();
        }
        else
            error ("Identifier or number or sub-expression expected");
        return result;
    }
 
    public Arit term ()
    {
        Arit result = factor ();
        while (isSeparator ("*") || isSeparator ("/"))
        {
            Arit temp = result;
            result = new Arit (token);
            nextToken();
            result.left = temp;
            result.right = factor ();
        }
        return result;
    }
 
    public Arit simpleExpr ()
    {
        Arit result = term ();
        while (isSeparator ("+") || isSeparator ("-"))
        {
            Arit temp = result;
            result = new Arit (token);
            nextToken();
            result.left = temp;
            result.right = term ();
        }
        return result;
    }
 
    public Arit expr ()
    {
        Arit result = simpleExpr ();
        if (isSeparator ("="))
        {
            Arit temp = result;
            result = new Arit (token);
            nextToken();
            result.left = temp;
            result.right = expr ();
        }
        return result;
    }
 
    public Arit ifStatement ()
    {
        checkKeyword("if");
        Arit result = new Arit ("if");
        checkSeparator ("(");
        result.left = expr (); // condition
        checkSeparator (")");
        result.middle = statement (); // then statement
        if (isKeyword ("else"))
        {
            nextToken (); // preskocit else
            result.right = statement (); // else statement
        }
        return result;
    }
 
    public Arit whileStatement ()
    {
        checkKeyword("while");
        Arit result = new Arit ("while");
        checkSeparator ("(");
        result.left = expr (); // condition
        checkSeparator (")");
        result.middle = statement (); // then statement
        return result;
    }
 
    public Arit statement ()
    {
        Arit result = null;
        if (isKeyword ("if"))
            result = ifStatement();
        else if (isKeyword ("while"))
            result = whileStatement();
        else 
        {
            result = expr ();
            checkSeparator(";");
        }
        return result;
    }
}
package example;
 
import java.util.*;
import java.lang.reflect.*;
import java.util.logging.Level;
import java.util.logging.Logger;
 
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.table.*;
 
public class Window extends javax.swing.JFrame {
 
    public Window() {
        initComponents();
    }
 
     private void run ()
    {
        Parser p = new Parser (input.getText ());
        output.setText ("");
        try
        {
            Arit a = p.statement();
            displayTree (a);
            output.append("O.K.");
        }
        catch (RuntimeException ex)
        {
            output.append(ex.toString ());
        }
 
    }
 
    DefaultTreeModel m; 
 
    private void displayTree (Arit a)
    {
       DefaultMutableTreeNode root = displayBranch (a) ;
       m = new DefaultTreeModel (root);
       tree.setModel (m);
    }
 
    private DefaultMutableTreeNode displayBranch (Arit a)
    {   
       DefaultMutableTreeNode node = new DefaultMutableTreeNode (a);
       if (a.left != null)
             node.add (displayBranch (a.left)) ;
       if (a.middle != null)
             node.add (displayBranch (a.middle)) ;
       if (a.right != null)
             node.add (displayBranch (a.right)) ;
       return node;
    } 
 
    DefaultTableModel tm;
 
    private void displayTable (Object obj)
    {
 
        Vector columns = new Vector ();
        columns.add ("Name");
        columns.add ("Value");
 
        Vector data = new Vector ();
 
        Class cls = obj.getClass();
        for (Field field : cls.getFields())
        {
           String name = field.getName ();
           String value = "";
 
            try 
            {
                Object answer = field.get (obj);
                if (answer != null)
                    value = answer.toString();
            }
            catch (IllegalArgumentException ex) { }
            catch (IllegalAccessException ex) { }
 
           Vector line = new Vector ();
           line.add (name);
           line.add (value);
           data.add (line);
        }
 
        tm = new DefaultTableModel (data, columns);
        table.setModel (tm);
 
    }
 
 
    private void runButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
        run ();
    }                                         
 
    private void treeValueChanged(javax.swing.event.TreeSelectionEvent evt) {                                  
        TreePath[] paths = evt.getPaths();
        for (TreePath path : paths)
            if (evt.isAddedPath(path))
            {
                Object obj = path.getLastPathComponent();
                if (obj instanceof DefaultMutableTreeNode)
                {
                    DefaultMutableTreeNode node = (DefaultMutableTreeNode) obj;
                    displayTable (node.getUserObject());
                }
            }
    }                                 
 
    private void runMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                            
        run ();
    }                                           
}
 
lexer2019.txt · Last modified: 2019/11/25 17:27 by 147.32.8.31
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki