嵌套循环时间分析(Nested Loops Time Analysis)
好的我正在学习算法课,正在为考试而学习......不幸的是......我无法理解嵌套循环时间分析背后的概念
这段代码中有三个循环
for (i=1->n) for (j=1->i) for (k=1->i) x=x+1;
我无法理解如何找到答案:s任何答案将是一个很好的帮助谢谢人们:)
Ok I am taking an Algorithm Class and am studying for the Exam... Unfortunately.. I can't understand the concept behind the nested loops time analysis
there are three loops in this code
for (i=1->n) for (j=1->i) for (k=1->i) x=x+1;
I can't understand how to figure out the answer :s Any Answer would be a great help Thanks Folks :)
原文:https://stackoverflow.com/questions/15176578
满意答案
你写的是想要检索procedure_declaration的解析,但是在你的代码中,你只想检索一个procedure_body,所以我假设这就是你想要的。
用我自己的话说,你想要编写语法而不告诉嵌入语法嵌入了哪种语法。 问题(在你的情况下没问题,因为你幸运地有一个非常友好的语法)这是在LALR(1)中,你需要一个前瞻标记来决定采取哪个规则。 你的语法看起来像这样:
procedure_declaration: SUB procedure_name EOS procedure_body END SUB EOS
您可以将procedure_name和procedure_body组合在一起,因此您的规则和语义操作将如下所示:
procedure_declaration: SUB combined = procedure_name EOS /* nothing here */ EOS { { procedure_name = fst combined; procedure_body = snd combined; } } procedure_name: id = IDENT { let lexbuf = _menhir_env._menhir_lexbuf in (id, Parser_pd.main Lexer_pd.token lexbuf) }
Parser_pd将包含此规则:
main: procedure_body END SUB { $1 }
您很可能在Parser_pd中想要END SUB,因为procedure_body可能不会自我分隔。
请注意,在解析过程名称标识符之后的第一个EOS之前调用子解析器,因为这是您的前瞻。 如果你在 EOS中调用它,则为时已晚,并且解析器已经从正文中提取了一个令牌。 第二个EOS是END SUB之后的EOS。
_menhir_env
显然是一个只适用于menhir的黑客。 您可能需要另一个hack来制作menhir --infer
work(如果您使用它),因为不希望用户引用它,因此符号不在范围内。 那个黑客将是:%{ type menhir_env_hack = { _menhir_lexbuf : Lexing.lexbuf } let _menhir_env = { _menhir_lexbuf = Lexing.from_function (* Make sure this lexbuf is never actually used. *) (fun _ _ -> assert false) } %}
You write that you want to retrieve the parsing of procedure_declaration, but in your code, you only want to retrieve a procedure_body, so I'm assuming that's what you want.
To put into my own words, you want to have to compose grammars without telling the embedding grammar which grammar is embedded. The problem (no problem in your case, because you luckily have a very friendly grammar) with this is that in LALR(1), you need one token of lookahead to decide which rule to take. Your grammar looks like this:
procedure_declaration: SUB procedure_name EOS procedure_body END SUB EOS
You can combine procedure_name and procedure_body, so your rule and semantic action will look like:
procedure_declaration: SUB combined = procedure_name EOS /* nothing here */ EOS { { procedure_name = fst combined; procedure_body = snd combined; } } procedure_name: id = IDENT { let lexbuf = _menhir_env._menhir_lexbuf in (id, Parser_pd.main Lexer_pd.token lexbuf) }
Parser_pd will contain this rule:
main: procedure_body END SUB { $1 }
You will very likely want END SUB in Parser_pd, because procedure_body is likely not self-delimiting.
Note that you call the sub-parser before parsing the first EOS after the procedure name identifier, because that is your lookahead. If you call it in EOS, it is too late, and the parser will have pulled a token from the body, already. The second EOS is the one after END SUB.
The
_menhir_env
thing is obviously a hack that only works with menhir. You may need another hack to makemenhir --infer
work (if you use that), because that doesn't expect a user to refer to it, so the symbol won't be in scope. That hack would be:%{ type menhir_env_hack = { _menhir_lexbuf : Lexing.lexbuf } let _menhir_env = { _menhir_lexbuf = Lexing.from_function (* Make sure this lexbuf is never actually used. *) (fun _ _ -> assert false) } %}
相关问答
更多使用ocamllex / ocamlyacc解析语法的一部分(Using ocamllex/ocamlyacc to parse part of a grammar)
解析XML文件的一部分(Parsing part of XML file)
创建一个包含令牌的表格.mly和.mll(Make a table containing tokens visible for both .mly an .mll)
突出显示mll和mly文件(Highlight mll and mly files)
用python解析一个url并改变其中的一部分(parsing a url in python with changing part in it)
通过单独生成.mly和.mll来检索解析的一部分(Retrieve a part of parsing by making separate .mly and .mll)
在ocamlbuild中使用.mly文件中的电池(Using Batteries in .mly file with ocamlbuild)
美元+标识符在mly(Dollar + identifier in mly)
相关文章
更多Solr参数(Analysis Collection Common CoreAdmin)
Solr官方文档系列——Text Analysis
Real-Time Rendering 笔记 --- 1-4章
The Log: What every software engineer should know about real-time data's unifying abstraction
计算机病毒及其防治 Computer Virus Analysis and Antivirus
zz Data Analysis Process
企业级搜索引擎Solr 第二章 Schema和文本分析(Schema and Text Analysis)[2]
java.lang.NoClassDefFoundError: org/apache/lucene/analysis/synonym/SynonymFilter
POJ 3620 Avoid The Lakes【DFS水题练格式Avoid The Lakes Time Limit: 1000MS Memory Limit: 65536K Total Sub】
科技英语翻译480句 (一)时间
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!