Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Parser;
- import Lexer.Lexem;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Stack;
- /**
- * Created by Vanyko on 09.05.18.
- */
- public class Parser {
- ArrayList<Lexem> lexems;
- public Parser(ArrayList<Lexem> lexems) {
- this.lexems = lexems;
- analyze();
- }
- public class MatchIdent implements MatchCallback {
- @Override
- public boolean match(int code) {
- return (code > 1000) || ((401 <= code) && (code <= 500));
- }
- }
- public class MatchUint implements MatchCallback {
- @Override
- public boolean match(int code) {
- return (501 <= code) && (code <= 1000);
- }
- }
- private State[] states = {
- new State( 0, "-<signal-program>", -1, null, 0, 0 ),
- new State( 1, "-<program>", 401, null, 0, 0 ),
- new State( 2, "", -52, null, 0, 0 ),
- new State( 3, "", 1, null, 0, 0 ),
- new State( 4, "", -6, null, 0, 0 ),
- new State( 5, "", 4, null, 0, 0 ),
- new State( 6, "-<block>", -10, null, 0, 0 ),
- new State( 7, "", 403, null, 0, 0 ),
- new State( 8, "", -32, null, 0, 0 ),
- new State( 9, "", 404, null, 0, 0 ),
- new State( 10, "", 402, null, 0, 0 ),
- new State( 11, "", -13, null, 0, 0 ),
- new State( 13, "-<declarations_list>", -16, null, 0, 0 ),
- new State( 14, "", -13, null, 0, 0 ),
- new State( 16, "-<declaration>", -51, null, 0, 0 ),
- new State( 17, "", 0, null, 0, 0 ),
- new State( 18, "", -24, null, 0, 0 ),
- new State( 19, "", -21, null, 0, 0 ),
- new State( 20, "", 1, null, 0, 0 ),
- new State( 21, "-<attributes_list>", -24, null, 0, 0 ),
- new State( 22, "", -21, null, 0, 0 ),
- new State( 24, "-<attribute>", 407, null, 0, 0 ),
- new State( 25, "", 408, null, 0, 0 ),
- new State( 26, "", 2, null, 0, 0 ),
- new State( 27, "", -29, null, 0, 0 ),
- new State( 28, "", 3, null, 0, 0 ),
- new State( 29, "-<range>", -31, null, 0, 0 ),
- new State( 30, "", 301, null, 0, 0 ),
- new State( 31, "", -44, null, 0, 0 ),
- new State( 32, "-<statements_list>", -35, null, 0, 0 ),
- new State( 33, "", -32, null, 0, 0 ),
- new State( 35, "-<statement>", -45, null, 0, 0 ),
- new State( 36, "", 302, null, 0, 0 ),
- new State( 37, "", -43, null, 0, 0 ),
- new State( 38, "", 1, null, 0, 0 ),
- new State( 39, "", 405, null, 0, 0 ),
- new State( 40, "", -32, null, 0, 0 ),
- new State( 41, "", 406, null, 0, 0 ),
- new State( 42, "", 1, null, 0, 0 ),
- new State( 43, "-<expression>", -45, null, 0, 0 ),
- new State( 44, "", 54, null, 0, 0 ),
- new State( 45, "-<variable>", -51, null, 0, 0 ),
- new State( 46, "", -47, null, 0, 0 ),
- new State( 47, "-<dimension>", 2, null, 0, 0 ),
- new State( 48, "", -43, null, 0, 0 ),
- new State( 49, "", 3, null, 0, 0 ),
- new State( 51, "-<variable_identifier>", -53, null, 0, 0 ),
- new State( 52, "-<procedure_identifier>", -53, null, 0, 0 ),
- new State( 53, "-<identifier>", 0, new MatchIdent(), 0, 0 ),
- new State( 54, "-<unsigned_integer>", 0, new MatchUint(), 0, 0 )
- };
- public class Tree {
- Tree root;
- String name;
- List<Tree> nodes;
- Tree(Tree root, String name) {
- this.root = root;
- this.name = name;
- this.nodes = new ArrayList<>();
- }
- };
- void PrintTree(Tree root, int prefix) {
- System.out.println(String.format("%" + prefix +"c%s", '\0', root.name));
- for (Tree node : root.nodes) {
- PrintTree(node, prefix + 4);
- }
- }
- public void analyze() {
- Stack<State> sp = new Stack<State>();
- int prefix = 0, ip = 0, lexemPointer = 0;
- Tree root = new Tree(null, "<root>");
- do {
- State stateToPush = getStateByIp(ip);
- if (stateToPush == null) return;
- sp.push(stateToPush);
- // System.out.println(sp.toString());
- if (!stateToPush.buffer.isEmpty()) {
- Tree node = new Tree(root, sp.peek().buffer);
- PrintTree(node, prefix += 4);
- root.nodes.add(node);
- root = node;
- }
- if (sp.peek().code < 0) {
- ip = -sp.peek().code;
- continue;
- }
- boolean accept = sp.peek().accept != null ? sp.peek().accept.match(lexems.get(lexemPointer).getCode()) : (lexems.get(lexemPointer).getCode() == sp.peek().code);
- if (!accept && (sp.peek().af != 1)) {
- System.out.println(String.format("error: %d -> %s\n", sp.peek().ip, lexems.get(lexemPointer).getValue()));
- return;
- }
- Tree node = new Tree(root, accept ? lexems.get(lexemPointer).getValue() : "empty");
- root.nodes.add(node);
- PrintTree(node, prefix);
- if (accept) {
- lexemPointer += 1;
- }
- if ((accept ? sp.peek().at : sp.peek().af) == 1) {
- while (!sp.empty() && ((accept ? sp.peek().at : sp.peek().af) != -1)) {
- if (!sp.peek().buffer.isEmpty()) {
- root = root.root;
- prefix -= 4;
- }
- sp.pop();
- }
- }
- if (!sp.empty()) {
- ip = sp.peek().ip + 1;
- if (sp.peek().buffer.isEmpty()) {
- sp.pop();
- }
- }
- } while (!sp.empty());
- PrintTree(root, 0);
- }
- State getStateByIp(int ip) {
- for (State state : states) {
- if (state.ip == ip) return state;
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment