简体   繁体   English

c++ - 如何扫描用户输入并将其切成块?

[英]How to scan through user input and cut it into chunks in c++?

I'm making a program to evaluate conditional proposition (~ or and -> <->).我正在制作一个程序来评估条件命题(~ 或和 -> <->)。 As the users input propositional variables and truth values (true, false) ,and proposition ;当用户输入命题变量真值(真,假)命题 the program will go through the inputs and return the truth value for the whole proposition.程序将遍历输入并返回整个命题的真值。

  • For ex: if i set p = true , q = true , r = false and input: p or q and r .例如:如果我设置p = true , q = true , r = false并输入: p 或 q 和 r
  • Is the anyway I can cut it into q and r first, then process and put it back to result (which is false ), then process the next bit ( p or false ) ??.无论如何,我可以先将它切成q 和 r ,然后处理并将其放回 result (这是false ),然后处理下一位( p 或false )??。 And it has to keep cutting out bits (In proper order of Precedence) and putting them back until I have left is a single true or false .并且它必须不断切割位(以正确的优先顺序)并将它们放回去直到我离开是一个true 或 false

    • And what I'm I supposed to use to hold user input (array, string) ???我应该用什么来保存用户输入(数组、字符串)???

    • Any help would be appreciated !任何帮助,将不胜感激 ! Thanks.谢谢。

Tasks like this are usually split into two phases, lexical analysis and syntactic analysis.像这样的任务通常分为两个阶段,词法分析和句法分析。

Lexical analysis splits the input into a stream of tokens .词法分析将输入拆分为标记流。 In your case the tokens would be the operators ~ , or , and , -> , <-> , variables and the values true , false .在您的情况下,标记将是运算符~orand-><-> 、变量和值truefalse You didn't mention them but I'd imagine you also want to include brackets as tokens in your language.你没有提到它们,但我想你也想在你的语言中包含括号作为标记。 Your language is simple enough that you could just write the lexical analyser yourself but tools such as flex or ragel might help you.您的语言非常简单,您可以自己编写词法分析器,但flexragel等工具可能会对您有所帮助。

Synyactic analysis is where you tease out the syntactic structure of your input and perform whatever actions you need (evaluate the preposition in your case).句法分析是您梳理输入的句法结构并执行您需要的任何操作(评估您的案例中的介词)的地方。 Syntactic analysis is more complex than lexical analysys.句法分析比词法分析更复杂。 You could write a recursive descent parser for this task, or you could use a parser generator to write the code for you.您可以为此任务编写递归下降解析器,也可以使用解析器生成器为您编写代码。 The traditional tool for this is called bison , but it's a bit clunky.用于此的传统工具称为bison ,但它有点笨重。 I like another simple tool called the lemon parser generator although it's more C orientated than C++.我喜欢另一个叫做柠檬解析器生成器的简单工具,尽管它比 C++ 更面向 C。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM