-2

I am new in prolog i have a code that i want to read from file then parse this is the code here

    var x;
    x <- (5 * 2);
   return (x + 1).

now in prolog i want to tokenize this like this first

 [’var’, ’x’, ’;’, ’x’,’<-’, ’(’, 5, ’*’, 2, ’)’, ’;’, ’return’, ’(’, ’x’, ’+’, 1, ’)’, ’.’]

the i want to implement predicate

 parse(+TokenList, -AST)

then again

evaluate(+AST, -Number)

using SWIProlog

The parser should not allow the keywords of the language (e.g., the arithmetic operators, <-, var, return) as variable identifiers

choroba
  • 231,213
  • 25
  • 204
  • 289
Nouman
  • 17
  • 6
  • prolog code and parser will be too in prolog – Nouman Feb 28 '17 at 09:02
  • ACTUALLY above code from text file is tokenized by prolog as in list after assigning to list we make predecated that is parse and get abstract syntax tree – Nouman Feb 28 '17 at 09:37
  • http://www.cs.utexas.edu/~jrellerm/courses/345/assignments/assignment2.pdf this is requirement – Nouman Feb 28 '17 at 09:48
  • The course materials should cover all you need. – choroba Feb 28 '17 at 10:06
  • can you tell me how to read file in PROLOG and tokenize it then remaining i will do it issue us to get tokkenization then parsing in PROLOG – Nouman Feb 28 '17 at 10:17
  • In order to parse, you need to write down what your grammar looks like (*e.g.*, `expression ::= term op expression | term`). If you haven't done that, it doesn't make sense to dive in to start parsing, especially when you're not familiar with the language. For Prolog specifically, check the Prolog manual for file I/O and token and atom predicates. You can also search the stackoverflow site for `[prolog] parser` and find lots of Q&A for examples. Once you have your grammar understood and have your Prolog documentation handy, you can write your DCG to align with the grammar. – lurker Feb 28 '17 at 12:43

1 Answers1

0

You can try tokenize_atom, to see if it fits your requirements. Otherwise, bite the bullet and try to write your own DCG...

CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • ok got it can you tell some of parsing rul of above code like i made 4 `mulOp(mulOp('/')) --> ['/'].` – Nouman Feb 28 '17 at 11:37
  • just rules as another is `addOp(addOp('+')) --> ['+'].` these are just parsing rules of code – Nouman Feb 28 '17 at 11:38