An issue of operator precedence in expressions when constructing grammars [citation]

Source: Internet
Author: User

1. Description

As a result of the lucky day of a perpetual calendar, the use of recursive descending grammar was considered as a prototype, a grammar was designed and then sent, and the article was collated.

Grammar is used to describe a language in which multiple sentences are included and sentences are made up of expressions. Multiple atomic expressions (non-a+b,c*d,-a expressions, such as:, etc.) and operators or parentheses together can be composed of composite expressions (such as: a+b*c,a* (B+C), etc.). It is important to note the operator precedence when writing expression grammars, such as: in Arithmetic, the multiplication is evaluated first, then the plus and minus, and the expression surrounded by parentheses has a higher precedence. To solve the priority problem, you can construct the grammar rules in the order of the lower priority operators to the higher precedence operators, since the parentheses have higher precedence relative to the operator, so the compound expression enclosed by parentheses can be treated as an atomic expression. Take arithmetic example below:

expr:expr'+' Term| Expr'-' Term|Term term:term'*'Factor| Term'/'Factor|Factor Factor:number|'('Expr')'

Both expr and term are compound expressions. Expr is the starting rule for expressions, handling the + and-operators, and excluding + and-cases. Term is used to denote the * and/operator and the case without * and/. Factor is an atomic expression that directly returns a lexical unit or a compound expression enclosed in parentheses.

2. Comparison Operators Walkthrough

The following constructs the grammar for the comparison operation, involving only ' > ', ' < ', ' = = ', '! = ' and logical operators ' && ' and ' | | '. We know that all comparison operators have the same precedence, and the ' && ' precedence in the logical operators is greater than ' | | ' High. So, similar to arithmetic's grammar, a composite expression of low priority is constructed first, and finally the highest-priority atomic expression. So the first is the description | | The rule, then the &&, and finally the atomic expression, according to the above, the grammar of the comparison operation can be constructed as:

compexpr:compexpr'||'andexpr|andexpr andexpr:andexpr'&&'Factor|Factor Factor:id'>' Number| Id'<' Number| Id'==' Number| Id'!=' Number|'('compexpr')'

3. Summary

Based on the grammar of arithmetic and comparison operations, we can summarize the construction principles of a priority expression grammar:
1. Construct rules from low-priority to high-priority order.
2. Each composite expression has an expression that contains only the next priority as the parsing option.
3. Parentheses have a high priority, and the conforming expression is enclosed in parentheses as an atomic expression.

An issue of operator precedence in expressions when constructing grammars [citation]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.