Post Reply 
Social Buttons
 
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hi Ed - what's new with your PL?
08-02-2018, 03:54 PM
Post: #11
RE: Hi Ed - what's new with your PL?
Yes it is Mitchell
You know the guy?
ok ... i never see before such a answer on stackoverflow
most of answers are to arogant and stupid but this one
is nice.
Find all posts by this user
Quote this message in a reply
08-02-2018, 09:02 PM
Post: #12
RE: Hi Ed - what's new with your PL?
(08-02-2018 03:54 PM)Aurel Wrote:  Yes it is Mitchell
You know the guy?
ok ... i never see before such a answer on stackoverflow
most of answers are to arogant and stupid but this one
is nice.

Yep. We're like brothers. We think very much alike. In fact, you might not be able to tell the difference between us.

And we both really like precedence climbing :-)

You would be surprised - there are actually lots of nice people on stackoverflow. But you are right - there are lots of nasty people there too.
Find all posts by this user
Quote this message in a reply
08-02-2018, 09:29 PM
Post: #13
RE: Hi Ed - what's new with your PL?
Quote:You would be surprised - there are actually lots of nice people on stackoverflow
probably but i like to avoid that place
I forget about one forum where are few people talk about
parsers ..especially one guy is really fine in explanations.
ahh ..internet....Big Grin
Find all posts by this user
Quote this message in a reply
10-29-2018, 05:15 AM
Post: #14
RE: Hi Ed - what's new with your PL?
Hi Ed !
Smile
Maybe you visit this forum from time to time
and i need your opinion about this situation:
Is possible to create simple recursive descent paser
on this way with just 3 function
I use something similar in ruben interpreter and work.
But i am not sure if will work with tokens
I mean first as you expect tokenize code into
token array (list) - ordinary string array
and parralel array which should hold token types
that should be ok ?
So then use this 3 function to parse expressions
..so what you think?
thanks in advance Smile

Code:
'fn expression -----------------------------------------------
function Expression() as Float
float res
if tokType = tNUM OR tokType = tVAR  'num or numeric variable
    if nextTokenType = tPLUS OR nextTokenType = tMINUS
        if tPLUS '(+)
            GetToken() ' get token from token list/array
            res = token + Term()
        elseif tMINUS '(-)
            GetToken()
            res = token - Term()
        end if
    end if
end if
return res
end function

'term -------------------------------------------------------
function Term() as Float
float res
res = Factor()
if nextTokenType = tMULTI OR nextTokenType = tDIVIDE
    if tMULTI '(*)
        GetToken()
        res = res * Factor()
    elseif tDIVIDE '(/)
        GetToken()
        res = res / Factor()
    end if
end if
return res
end function

'factor -----------------------------------------------------
function Factor() as Float
float res
if tokType = tNUM
    return GetNum(token)
end if

if tokType = tVAR
    return GetVar(token)
end if

if tokType = tLParen '(
Match("(")
res = Expression()
Match(")")
end if
'else?
return res ' from func->Expression()
end function
Find all posts by this user
Quote this message in a reply
10-31-2018, 04:39 AM
Post: #15
RE: Hi Ed - what's new with your PL?
and just to add code by Sven from Stackoverflow

Lookahead
command() {
if (currentToken() == id && lookaheadToken() == '=') {
return assignment();
} else {
return expr();
}
}


this also should work with tokenType

if tokType = tVAR || tokType = tSTR && nextTokType = tEQUAL
ret Assign()
else
ret Expr()
end if
Find all posts by this user
Quote this message in a reply
10-31-2018, 05:22 AM
Post: #16
RE: Hi Ed - what's new with your PL?
This one looking nice and clear and I must test it:
it is from :
http://www.cs.ecu.edu/karl/5220/spr16/No...scent.html

The following example not only parses simple expressions but also does some semantic processing: it evaluates the expression.

Code:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <limits.h>

/* This is an example of a
* recursive descent parser.
* It evaluates an expression
* using the following
* grammar.
*
* Expr     -> Term ExprRest
*
* ExprRest -> (empty)
*          |  + Expr
*          |  - Expr
*
* Term     -> Factor TermRest
*
* TermRest -> (empty)
*          |  * Term
*          |  / Term
*
* Factor   -> number
*          |  ( Expr )
*
* where a number is an
* integer and all operations
* are done using integer
* arithmetic.
*/

#define MAX_DIGITS 10
#define TOK_NUMBER 256

typedef int TOKEN;

TOKEN lookahead = 0;
int   attribute = 0;
int   nextch    = EOF;

void syntaxerror(void);
void advance(void);
TOKEN lex(void);
void match(TOKEN t);
int Expr(void);
int ExprRest(int n);
int Term(void);
int TermRest(int n);
int Factor(void);

/*============================
* syntaxerror():
* Report a syntax error.
*
* Error reporting is very
* crude here.
*============================*/

void syntaxerror(void)
{
  printf("Syntax error");
  exit(1);
}


/*============================
* advance():
* Skip over white space,
* then read a character.
*
* Store the character that
* was read into nextch.
*============================*/

void advance(void)
{
  nextch = getchar();
  while(isspace(nextch)) {
    nextch = getchar();
  }
}

/*===========================
* lex():
* A simple ad-hoc lexer
*
* Read a token and store it
* into lookahead.
*===========================*/

TOKEN lex(void)
{
  if(nextch == EOF) {
    return EOF;
  }

  if(nextch == '+' ||
     nextch == '-' ||
     nextch == '*' ||
     nextch == '/' ||
     nextch == '(' ||
     nextch == ')') {

    int tok = nextch;
    advance();
    return tok;
  }

  if(isdigit(nextch)) {
    int v = 0;

    while(isdigit(nextch)) {
      v = 10*v + (nextch - '0');
      advance();
    }
    attribute = (int) v;
    return TOK_NUMBER;
  }

  syntaxerror();
  return 0;
}

/*===========================
* match(t):
* Check that the current
* lookahead is t.
*
* If so, get the next token
* (which becomes the next
* lookahead token).
*
* If there is a mismatch,
* it is a syntax error.
*===========================*/

void match(TOKEN t)
{
  if(lookahead == t) {
    lookahead = lex();
  }
  else {
    syntaxerror();
  }
}

/*===========================
* Expr():
* Read an expression
* and return its value.
*
* Expr -> Term ExprRest
*===========================*/

int Expr(void)
{
  int x = Term();
  int y = ExprRest(x);
  return y;
}
  
/*===========================
* ExprRest(n):
*
* ExprRest
*   -> (empty)
*         Return n
*   |  + Expr
*         Return n + val(Expr)
*   |  - Expr
*         Return n - val(Expr)
*===========================*/

int ExprRest(int n)
{
  int x;

  if(lookahead == '+') {
    match('+');
    x = Expr();
    return n + x;
  }

  if(lookahead == '-') {
    match('-');
    x = Expr();
    return n - x;
  }

  return n;
}
  
/*===========================
* Term():
* Read a term and
* return its value.
*
* Term -> Factor TermRest
*===========================*/

int Term(void)
{
  int x = Factor();
  int y = TermRest(x);
  return y;
}  

/*===========================
* TermRest(n):
*
* TermRest
*   -> (empty)
*         Return n
*   |  * Term
*         Return n * val(Term)
*   |  / Term
*         Return n / val(Term)
*===========================*/

int TermRest(int n)
{
  int x;

  if(lookahead == '*') {
    match('*');
    x = Term();
    return n * x;
  }

  if(lookahead == '/') {
    match('/');
    x = Term();
    return n / x;
  }

  return n;
}
  
/*===========================
* Factor():
* Read a factor and return
* its value.
*
* Factor   -> number
*          |  ( Expr )
*===========================*/

int Factor(void)
{
  int x;

  if(lookahead == '(') {
    match('(');
    x = Expr();
    match(')');
    return x;
  }

  if(lookahead == TOK_NUMBER) {
    x = attribute;
    match(TOK_NUMBER);
    return x;
  }

  syntaxerror();
  return 0;
}
  
/*===========================
* main():
* Read an expression and
* print its value, or print
* "syntax error" if the
* expression is poorly formed.
*
* The expression ends at the
* end of file.
*===========================*/

int main()
{
  int val;

  /* Prime the lexer. */

  advance();
  lookahead = lex();

  /* Evaluate the expression. */

  val = Expr();
  if(lookahead != EOF) {
    printf("%d\n", val);
  }
  else {
    syntaxerror();
  }

  return 0;
}
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: