Advertisement
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;
- }
- }
- 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, "", -59, null, -1, 0 ),
- new State( 3, "", 1, null, 0, 0 ),
- new State( 4, "", -6, null, -1, 0 ),
- new State( 5, "", 4, null, 1, 0 ),
- new State( 6, "-<block>", -7, null, 0, 0 ),
- new State( 7, "", -11, null, -1, -1 ),
- new State( 8, "", 403, null, 0, 0 ),
- new State( 9, "", -36, null, -1, 0 ),
- new State( 10, "", 404, null, 0, 0 ),
- new State( 11, "<variable-declarations>", 402, null, 0, 0 ),
- new State( 12, "", -13, null, 0, 0 ),
- new State( 13, "-<declarations_list>", -14, null, 0, 0 ),
- new State( 14, "", -16, null, -1, 0 ),
- new State( 15, "", -14, null, 0, 0 ),
- new State( 16, "-<declaration>", -17, null, 0, 0 ),
- new State( 17, "", -58, null, -1, 0 ),
- new State( 18, "", 0, null, 0, 0 ),
- new State( 19, "", -21, null, -1, -1 ),
- new State( 20, "", 1, null, 1, 0 ),
- new State( 21, "-<attributes_list>", -22, null, 0, 0 ),
- new State( 22, "", -24, null, 1, 0 ),
- new State( 23, "", -22, null, 0, 0 ),
- new State( 24, "-<attribute>", -25, null, 0, 0 ),
- new State( 25, "", -30, null, 0, -1 ),
- new State( 26, "", -31, null, 0, -1 ),
- new State( 27, "", 2, null, 0, 1 ),
- new State( 28, "", -32, null, -1, 0 ),
- new State( 29, "", 3, null, 1, 0 ),
- new State( 30, "<int>", 407, null, 1, 1 ),
- new State( 31, "<float>", 408, null, 1, 1 ),
- new State( 32, "-<range>", -33, null, 0, 0 ),
- new State( 33, "", -61, null, -1, 0 ),
- new State( 34, "", 301, null, 0, 0 ),
- new State( 35, "", -61, null, 0, 0 ),
- new State( 36, "-<statements_list>", -37, null, 0, 0 ),
- new State( 37, "", -39, null, -1, 0 ),
- new State( 38, "", -37, null, 0, 0 ),
- new State( 39, "-<statement>", -40, null, 0, 0 ),
- new State( 40, "", -45, null, 0, -1 ),
- new State( 41, "", -52, null, -1, -1 ),
- new State( 42, "", 302, null, 0, 0 ),
- new State( 43, "", -49, null, -1, 0 ),
- new State( 44, "", 1, null, 1, 0 ),
- new State( 45, "", 405, null, 0, 1 ),
- new State( 46, "", -36, null, 0, 0 ),
- new State( 47, "", 406, null, 0, 0 ),
- new State( 48, "", 1, null, 1, 0 ),
- new State( 49, "-<expression>", -50, null, 0, 0 ),
- new State( 50, "", -52, null, -1, -1 ),
- new State( 51, "", -61, null, 0, 0 ),
- new State( 52, "-<variable>", -53, null, 0, 0 ),
- new State( 53, "", -58, null, -1, 0 ),
- new State( 54, "", -55, null, 0, 0 ),
- new State( 55, "-<dimension>", 2, null, 0, 1 ),
- new State( 56, "", -49, null, -1, 0 ),
- new State( 57, "", 3, null, 1, 0 ),
- new State( 58, "-<variable_identifier>", -60, null, 0, 0 ),
- new State( 59, "-<procedure_identifier>", -60, null, 0, 0 ),
- new State( 60, "-<identifier>", 0, new MatchIdent(), 1, 1 ),
- new State( 61, "-<unsigned_integer>", 0, new MatchUint(), 1, 1 )
- };
- 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 = 1, 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) - 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
Advertisement