Git Product home page Git Product logo

Comments (3)

jamestjw avatar jamestjw commented on August 20, 2024

I agree with your take on this, below is a snippet of operator precedence from the SubC compiler

static int Prec[] = {
	7, /* SLASH */
	7, /* STAR */
	7, /* MOD */
	6, /* PLUS */
	6, /* MINUS */
	5, /* LSHIFT */
	5, /* RSHIFT */
	4, /* GREATER */
	4, /* GTEQ */
	4, /* LESS */
	4, /* LTEQ */
	3, /* EQUAL */
	3, /* NOTEQ */
	2, /* AMPER */
	1, /* CARET */
	0, /* PIPE */
};

and indeed we can see that the comparison operators have lesser precedence than the arithmetic operators, and the equality operators have lesser precedence than the comparison operators

from acwj.

mxysfive avatar mxysfive commented on August 20, 2024

I also meet this problem. When I input 2 + 7 < 9 the author's code return 3 rather than 0.

from acwj.

vcheckzen avatar vcheckzen commented on August 20, 2024

Yes, the author could have done the wrong thing. The precedence order is not consistent with this doc which is even mentioned by the author in Readme. In this chapter, the precedence table should be adjusted to the following.

// Operator precedence for each token. Must
// match up with the order of tokens in defs.h
// Larger ones should first be caculated.
static int OpPrec[] = {
   0,                           // T_EOF
  30, 30,                       // T_PLUS, T_MINUS
  40, 40,                       // T_STAR, T_SLASH
  10, 10,                       // T_EQ, T_NE
  20, 20, 20, 20                // T_LT, T_GT, T_LE, T_GE
};

from acwj.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.