什么茶叶能减肥刮油脂肪| 女生什么时候容易怀孕| 酸中毒是什么意思| 氯化钙是什么| 忧心忡忡是什么意思| 益安宁丸主治什么病| 为什么会突然吐血| 尿泡沫多是什么原因| 脚心发凉是什么原因| 筹钱是什么意思| 二代身份证是什么意思| 玥字属于五行属什么| 伤元气是什么意思| 蜈蚣吃什么食物| 阳虚有什么症状| 警察在古代叫什么| 壑是什么意思| 脸上长黑斑是什么原因引起的| 踩指压板有什么好处| 过是什么结构的字| 竞走是什么意思| 洗手指征是什么| 今天生日什么星座| qty什么意思| 黄褐斑是什么引起的| pls是什么意思| 橱窗是什么意思| 副歌部分是什么意思| fq交友是什么意思| 小孩晚上睡觉流口水是什么原因| 吹空调咳嗽是什么原因| 血糖高是什么原因引起| 密度单位是什么| 禅心是什么意思| 为什么经常刷牙还牙黄| 晚上减肥吃什么| 怀孕肚子上长毛是什么原因| 木指什么生肖| 沐什么意思| 唯女子与小人难养也什么意思| 榴莲有什么品种| 梦到被蛇咬是什么意思周公解梦| 仔仔是什么意思| 白头翁代表什么生肖| 重字五行属什么| 白带带血丝是什么原因| 异常灌注是什么意思| 鸽子红鼻头喂什么药| jet是什么意思| 什么脸型适合什么发型| 高烧不退有什么好办法| 乳糖是什么糖| miki是什么意思| 肾的主要功能是什么| 左胸上方隐痛什么原因| 腿脚肿胀是什么原因引起的| 不可以加什么偏旁| 棘手是什么意思| 苦荞茶有什么功效| 茉莉什么时候开花| 撤退性出血是什么意思| 减肥喝什么饮料| 为什么洗头发时会掉很多头发| 额头出汗多是什么原因| 洗牙后要注意什么| 小腿肚子抽筋是什么原因| 食管炎吃什么药| r一谷氨酰转移酶高说明什么| 友女是什么意思| 什么叫道德| 肌钙蛋白高是什么原因| 雪霁是什么意思| 公貔貅和母貔貅有什么区别| 往事不堪回首是什么意思| 喝柠檬水有什么好处和坏处| npv是什么病毒| 睾丸疼痛吃什么药最好| 金针菇为什么叫明天见| 生命的真谛是什么| 嘴唇上长水泡是什么原因| 冬眠的动物有什么| 梦见和死人一起吃饭是什么意思| 南京是什么省| 天启是什么意思| 什么是菊粉| bv什么意思| 感冒了吃什么水果比较好| 荨麻疹有什么症状| 女人练瑜伽有什么好处| 低压高吃什么药最有效| 财代表什么生肖| 茶叶五行属什么| 哀莫大于心死什么意思| 耳塞戴久了有什么危害| 大便一粒粒的是什么原因| 匝道是什么| l什么意思| 阴囊两侧瘙痒是什么原因| aed什么意思| 申时是什么时间| 为什么叫老鸨| 梦到公鸡是什么意思| 早餐吃什么最营养| 兔子能吃什么| 冲鸡蛋水喝有什么好处| 口干舌燥喝水也不解渴是什么原因| 初衷是什么意思| 火红的什么| 呼吸内镜检查什么| 眩晕症是什么原因引起的| jackie是什么意思| 列装是什么意思| 2月4号是什么星座| 小腹右边疼是什么原因| 跑步大腿痒是什么原因| 扁桃体切除有什么影响| 吃什么会自然流产| hermes是什么意思| 肉桂跟桂皮有什么区别| 发际线是什么| 禾加术念什么| 什么字笔画最多| 闭关是什么意思| 鼓风机是干什么用的| 劫财代表什么| 胱抑素是什么| c8是什么意思| 什么叫囊肿| 尿酸检查什么项目| 老年斑是什么原因引起的| 梦到吃饭是什么意思| 为什么会晕血| 喝完酒吃点什么对胃好| 血管堵塞吃什么好疏通| 跛子是什么意思| 阿尔卑斯是什么意思| 胃胀是什么原因| 春什么秋什么的成语| 刀客是什么意思| 一直腹泻是什么原因| 前囟门什么时候闭合| 盆腔炎用什么药好| 动脉夹层什么意思| 什么是癔症| 近义词是什么意思| 母亲节送给妈妈什么礼物| 大脑记忆力下降是什么原因| 龙阳之好是什么意思| 鼻塞一直不好什么原因| 大姨妈喝什么汤好| 岁月静好什么意思| 酵母菌是什么菌| 湿疹和热疹有什么区别| 治疗舌苔白厚用什么药| 海松茸是什么东西| 贝字旁与什么有关| k是什么| 教学相长是什么意思| 毛字出头念什么| 竹荪是什么| 什么叫服务贸易| 古丽是什么意思| 666什么意思| 鼠妇是什么| 五月天主唱叫什么名字| 心脏支架和搭桥有什么区别| 农历七月初七是什么节日| 什么空如什么| 什么的童话| 右肩膀疼是什么原因| 第一次要注意什么| 玉戴久了会有什么变化| 先兆流产是什么意思| 伤风胶囊又叫什么| 乳腺应该挂什么科| 为什么说有钱难买孕妇B| 睾丸胀痛什么原因| 乳腺增生不能吃什么食物| 胖子适合什么发型| 二级警督是什么级别| 红疮狼斑是什么引起的| 银子为什么会变黑| 曹操原名叫什么| 高岭土是什么| 心慌吃什么药能缓解| 杰五行属性是什么| 澳大利亚人说什么语言| 定海神针什么意思| 木梳子梳头有什么好处| 腺样体肥大有什么症状| 焦虑挂什么科| 81岁属什么| 果酱样大便见于什么病| 西瓜像什么| 古人的婚礼在什么时间举行| 爱字五行属什么| 猪生肠是什么部位| 老茧是什么意思| 什么是哺乳动物| 孕妇晚上饿了吃什么好| 吉士粉是什么粉| 中医行业五行属什么| 什么水果是碱性的| 维生素b6是治什么的| 兔子的眼睛为什么是红色的| 生理需要是什么意思| 粘胶是什么面料| 丑时五行属什么| 上天是什么意思| 无事不登三宝殿什么意思| Ca是什么| 梦见袜子破了是什么意思| 马甲线是什么意思| 1976年出生属什么生肖| 陈醋和香醋有什么区别| 一月二十号是什么星座| 令妹是什么意思| 每天早上起床口苦是什么原因| marni是什么牌子| 天秤男和什么星座最配| 寒冷性荨麻疹是什么原因引起的| prc是什么意思| 几又念什么| 才美不外见的见是什么意思| 钙化什么意思| 内敛是什么意思| 117是什么意思| 肺和大肠相表里是什么意思| 什么是云| 为什么经常打嗝| 中国的国球是什么| 黑色柳丁是什么意思| 农历9月28日是什么星座| 为什么会全身酸痛| 曝光是什么意思| 小孩发烧呕吐是什么原因| 艾拉是什么药这么贵| 什么丝什么缕| 三杯鸡为什么叫三杯鸡| 活死人是什么意思| 三百年前是什么朝代| 价值是什么| 便秘吃什么快速通便| 蜈蚣属于什么类动物| b超涂的液体是什么| 不来事是什么原因| 7月6号是什么星座| 喝隔夜茶有什么好处和坏处| 小日子是什么意思| 孕妇喝咖啡有什么危害| alex是什么意思| 桃花像什么| 什么程度算精神出轨| 睾丸疝气有什么症状| 银杏树的叶子像什么| 为什么会过敏| 老子叫什么| 菠菜为什么要焯水| 番薯是什么| 葡萄糖偏高是什么原因| 什么心什么胆| 扑救带电火灾应选用什么灭火器| 什么茶降尿酸| 康熙雍正乾隆是什么关系| 百度
Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

On Parsing Perl

by merlyn (Sage)
on Dec 04, 2000 at 04:21 UTC ( [id://44722]=perlmeditation: print w/replies, xml ) Need Help??

{from an alt.perl post I just made, reposted here to solicit feedback from fellow monks...}

>>>>> "Makhno" == Makhno <mak@imakhno.freeserve.co.uk> writes: Makhno> I'm thinking of writing a GUI Perl-syntax-aware editor, and Makhno> wondering what's the best way to parse perl? Highlighting Makhno> reserved words is easy (using, eg, index()) but indentifying Makhno> things like comments is a bit more difficult. Makhno> A regex like /#.*\n/ will catch comments when they are used Makhno> simply, ie: Makhno> print "hello\n"; #print hello Makhno> but will get it wrong when the '#' is used as part of a regex Makhno> (or in a string) Makhno> s#hello#goodbye#; Makhno> print "will behave like a #comment"; Makhno> Does anybody have any ideas on how I go about parsing perl Makhno> syntax in such a way, before I go to a lot of potentially Makhno> unnecessary work?
Perl is extremely difficult to parse. In fact, some would say impossible.

One thing that makes it difficult is the dual nature of a half dozen characters like "/". If that / is being used in a place that's expecting an operator, it's divide. If it's being used in a place that's expecting an operand, it's the beginning of a regular expression. So you have to keep track at all times of whether you're looking for an operator or an operand.

"No problem", you say? Quick... for the following, play the game of "regex or divide?"

sin / ... time / ... localtime / ... caller / ... eof / ...
Got those right? How about these?
use constant FOO => 35; FOO / ... use Fcntl qw(LOCK_SH); LOCK_SH / ...
OK, and now some of your own:
sub no_args (); sub one_arg ($); sub normal (@); no_args / ... one_arg / ... normal / ...
Got those too? How about these (same problem, different file):
use Random::Module qw(aaa bbb ccc); aaa / ... bbb / ... ccc / ...
A little harder, eh? So now you have to parse OUTSIDE the file to get your answer. And as if that wasn't enough, let's get weird:
BEGIN { eval (time % 2 ? 'sub zany ();' : 'sub zany (@);'); } zany / ...
Quick, was that last one a divide or a regex start?

Why does it matter? Look at this:

sin / 25 ; # / ; die "this dies!"; time / 25 ; # / ; die "this doesn't die";
The first one is computing the sin of the true/false value gotten by matching " 25 ; # " against $_. Then it dies. The second one is computing the time of day divided by 25, then ignoring the comment.

Starting to see the trouble?

This leads people to say "the only thing which can parse Perl (the language) is perl (the binary)". Maybe not for Perl6. But for the Perl we know and can use today, certainly so.

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
Re: On Parsing Perl
by quidity (Pilgrim) on Dec 04, 2000 at 04:40 UTC

    I do most of my perl coding using CPerl mode for Xemacs, and although it is very good at spotting syntax it is often horribly wrong, especially when odd quoting characters or pod is brought into the equation. eval is even worse. I'd advise anyone even thinking of trying to parse perl to look at what can be achieved, and then either improving that (to the benefit of everyone) or just to give up.

    I do sometimes find myself chosing a particular way of coding over another (possibly better) way because the second breaks the pretty printing, and I want others using the same editor to be able to read the code I write.

      I hate to say this, but even as an XEmacs fan, cperl+gemacs is far superior. Recent GNU Emacs has some extra stuff that XEmacs doesn't have, that allows cperl to do some really amazing things. (I have vague memories of the "extra stuff" being multiple syntax transition tables for each character, so you can gracefully handle things like m!!x and other non-standard delimiters. But I could be totally wrong.)
Re: On Parsing Perl
by repson (Chaplain) on Dec 04, 2000 at 07:28 UTC
    You could use B::Deparse for some of it, which eliminates some of what you don't want, but even that fails on many other things. The best bet is to code for the majority of perl and leave programmers to use their heads for the rest. This is what I do with syntax highlighting in vim, I use it generally but don't belive it for a moment. It is still sometimes helpful anyway. This is the way it will have to stay for now, at least until Perl6...
Re: On Parsing Perl
by toadi (Chaplain) on Dec 04, 2000 at 13:49 UTC
    I'm with you merlyn. I use vim(*nix) and textpad(windows), both make some mistakes. Like in some regex syntax it does some weird things...


    --
    My opinions may have changed,
    but not the fact that I am right

Re: On Parsing Perl
by nop (Hermit) on Dec 04, 2000 at 21:21 UTC
    I use the perl mode on emacs, and resort to small tricks to keep everything ok. For example,
    s/'"/;
    upsets the syntax colorization badly (as emacs thinks following code is in the string), so I use idioms like
    s/'"/; #"'
    to "close" my "open" strings....
Eight years later...
by samwyse (Scribe) on Jan 13, 2009 at 19:08 UTC
    I decided to run this test script under various versions of Perl.
    @examples = split /\n/, <<'EXAMPLES'; sin / ... time / ... localtime / ... caller / ... eof / ... use constant FOO => 35; FOO / ... use Fcntl qw(LOCK_SH); LOCK_SH / ... sub no_args (); sub no_args{1}; no_args / ... sub one_arg ($); sub one_arg{1}; one_arg / ... sub normal (@); sub normal{1}; normal / ... EXAMPLES for (@examples) { s=\.\.\.=25 ; # / ; die "this dies!";=; local($a) = eval; $a = $@ if $@; print "$_\n\t$a\n"; }
    I don't know what the results would be for earlier versions, but from Perl 5.6 onwards it's pretty consistent.
    Example5.0065.0085.010
    sindiesdiesdies
    time49274891.7249274891.7449274891.76
    localtimediesdiesdies
    callerdiesdiesdies
    eofdiesdiesdies
    FOO1.41.41.4
    LOCK_SHdiesdies0.04
    no_argsdiesdiesdies
    one_argdiesdiesdies
    normaldiesdiesdies
    Most of the "dies" instances also produced this message: Warning: Use of "XXX" without parentheses is ambiguous at (eval N) line 1. However, the LOCK_SH example never generated errors, while the last three generated "Prototype mismatch" messages. I must also note that almost all of the examples generated warnings, despite using neither the '-w' option or 'use strict;'
Re: On Parsing Perl (Once upon a time)
by Anonymous Monk on Jul 11, 2022 at 11:27 UTC
    I'm currently working on something, (basically perl parser), and apart from the BEGIN block, everything seems parseable (is that the right word?) using some simple LL grammars. Or am I just too uneducated?
      everything seems parseable (is that the right word?) using some simple LL grammars.

      No, only perl (the interpreter) can parse all of Perl (the language). See my node here for details.

      Edit: added emphasis.

      Static parsing is only reliable, if you rule out or control all imported subs, because prototypes change the way Perl is parsed. See HaukeX's other reply.

      Basically changes at compile time ( see BEGIN blocks ) can change the parser.

      Dynamic parsing is possible though, if you inspect the op-tree after compilation, that's the basic idea of some newer tools, like the perlnavigator.

      See also perl -c in perlrun or B::Xref

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        Static parsing is only reliable, if you rule out or control all imported subs, because prototypes change the way Perl is parsed.

        I think, though, that prototypes aren't the only reason Perl isn't statically parseable. There are quite a few heuristics that the parser uses that aren't all too well documented, and I'm not sure if a static parser would be able to reimplement all of them. And then there is no strict code, which I think gets even trickier. At some point I was considering researching and making a list of all of the reasons, but I unfortunately never got around to it.

Re: On Parsing Perl
by gaggio (Friar) on Dec 04, 2000 at 04:35 UTC
    I don't know if I am with you there. What is the Perl executable doing when it executes a script? Isn't that called parsing also?
      Stunning reading comprehension there. I wonder if such clueless folk ever come back and read their comments years later and feel a twinge of embarrassment.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://44722]
Approved by root
Front-paged by htoug
help
Chatterbox?
and all is quiet...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2025-08-06 04:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.


    双子座的幸运色是什么 便秘用什么药效果好 头皮挂什么科 草字头加叔念什么 王晶为什么不娶邱淑贞
    镇关西是什么意思 四大天王是什么生肖 尿酸高适合吃什么水果 紫花地丁有什么功效 闰月是什么意思
    血小板太高会导致什么 tat是什么意思 菠菜不能与什么一起吃 1919年发生了什么 经常手麻是什么原因
    什么样的山峰 3月16日是什么星座 盐酸达泊西汀片是什么药 做完痔疮手术吃什么好 阴唇为什么一大一小
    什么东西补血最快hcv8jop6ns8r.cn 阿尔茨海默症是什么hcv9jop3ns3r.cn 残联是什么性质的单位hcv8jop1ns6r.cn 徐长卿是什么药hcv8jop5ns0r.cn 透析是什么意思啊hcv7jop7ns1r.cn
    透析病人磷高了吃什么降磷hcv8jop8ns8r.cn 吃海带有什么好处和坏处cl108k.com 曲安奈德针治疗什么hcv9jop1ns7r.cn 低血糖会出现什么症状hcv8jop0ns6r.cn 舅子是什么意思hcv9jop4ns7r.cn
    磁共振和ct有什么区别hcv7jop9ns3r.cn exm是什么意思hcv9jop4ns0r.cn 一什么香蕉hcv8jop2ns3r.cn 户籍地是什么hcv9jop4ns3r.cn 鱼刺卡喉咙去医院挂什么科imcecn.com
    9月9号是什么星座hcv8jop7ns4r.cn 怀孕的最佳时间是什么时候hcv7jop6ns3r.cn 杨梅有什么好处hcv8jop8ns8r.cn 放疗有什么副作用hcv9jop4ns2r.cn 心率变异性是什么意思hcv8jop9ns9r.cn
    百度